-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorder_list.cpp
107 lines (91 loc) · 2.19 KB
/
reorder_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// https://oj.leetcode.com/problems/reorder-list/
// Date: O8th Nov, 2014
#include <iostream>
#include <stack>
#include <vector>
struct ListNode
{
int val;
ListNode* next;
ListNode(int x): val(x), next(NULL) {}
};
class Solution
{
public:
void reorderList(ListNode *head)
{
int size_list = sizeList(head);
if (size_list <= 2)
{
return;
}
int count_skip = size_list - (size_list - 1)/2;
//ListNode* h1 = head;
//ListNode* h2 = head;
ListNode* curNode = head;
ListNode* tail = NULL;
// skip the elements till we reach the later half of the list which needs to be moved.
int count_elem = 0;
while (count_elem < count_skip)
{
tail = curNode;
curNode = curNode->next;
++count_elem;
}
std::stack<ListNode*> stk;
while (curNode)
{
stk.push(curNode);
curNode = curNode->next;
}
// now pop the stack and re-order the list
ListNode* prevNode = head;
while (!stk.empty())
{
ListNode* stkNode = stk.top();
stk.pop();
ListNode* nextNode = prevNode->next;
prevNode->next = stkNode;
stkNode->next = nextNode;
prevNode = nextNode;
}
tail->next = NULL;
}
private:
int sizeList(ListNode* head)
{
ListNode* node = head;
int size_list = 0;
while (node)
{
size_list += 1;
node = node->next;
}
return size_list;
}
};
int main(int argc, char* argv[])
{
int arr[] = {1,2,3,4};
std::vector<int> vec(arr, arr+sizeof(arr)/sizeof(arr[0]));
// build linked list
ListNode* head = NULL;
ListNode* curNode = NULL;
for (int i=0; i != vec.size(); ++i)
{
if (head)
{
ListNode* newNode = new ListNode(vec[i]);
curNode->next = newNode;
curNode = newNode;
}
else
{
curNode = new ListNode(vec[i]);
head = curNode;
}
}
Solution sln;
sln.reorderList(head);
return 0;
}