-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreorderList.cpp
More file actions
executable file
·60 lines (53 loc) · 1.33 KB
/
reorderList.cpp
File metadata and controls
executable file
·60 lines (53 loc) · 1.33 KB
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
/*
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.
*/
#include "ulti.h"
using namespace std;
void reorderList(ListNode *head){
if(head == NULL || head->next==NULL || head->next->next==NULL)
return;
ListNode *mid = head;
ListNode *pt = head;
while(pt->next && pt->next->next){
mid = mid->next;
pt = pt->next->next;
}
pt = mid->next;
while(pt->next){
ListNode *tmp = mid->next;
mid->next = pt->next;
pt->next = pt->next->next;
mid->next->next = tmp;
}
pt = head;
while(mid!=pt && mid->next){
ListNode *tmp = pt->next;
pt->next = mid->next;
mid->next = mid->next->next;
pt->next->next = tmp;
pt = tmp;
}
return;
}
int main(int argc, char const *argv[])
{
vector<int> ary={1,2,3,4,5,6,7,8};
ListNode *head = constructList(ary);
ListNode *tp = head;
cout << "Unsorted List\n";
while(tp){
cout << tp->val << " ";
tp = tp->next;
}
reorderList(head);
cout << "\nReordered List\n";
while(head){
cout << head->val << " ";
head = head->next;
}
return 0;
}