-
Notifications
You must be signed in to change notification settings - Fork 1
/
143 Reorder List .cpp
62 lines (62 loc) · 1.57 KB
/
143 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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if (!head) {
return ;
}
auto getLen = [] (ListNode *x) {
int s = 0;
for (; x; x = x->next) {
s++;
}
return s;
};
int len = getLen(head);
int halflen = (len + 1) >> 1;
ListNode *head1 = head, *head2 = head;
for (int i = 0; i + 1 < halflen; i++) {
head2 = head2->next;
}
ListNode *next = head2->next;
head2->next = nullptr;
head2 = next;
auto reverse = [] (ListNode *x) {
ListNode *newHead = nullptr;
ListNode *next;
for (; x; x = next) {
next = x->next;
x->next = newHead;
newHead = x;
}
return newHead;
};
head2 = reverse(head2);
ListNode *p = head1, *q = head2;
ListNode *newHead = nullptr, *newTail = nullptr;
int cnt = 0;
while (p) {
if (!newHead) {
newHead = newTail = p;
p = p->next;
} else {
newTail->next = p;
newTail = p;
p = p->next;
}
if (q) {
newTail->next = q;
newTail = q;
q = q->next;
}
}
newTail->next = nullptr;
}
};