forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0143-reorder-list.cpp
75 lines (63 loc) · 1.72 KB
/
0143-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
/*
Given head of linked-list, reorder list alternating outside in
Ex. head = [1,2,3,4] -> [1,4,2,3], head = [1,2,3,4,5] -> [1,5,2,4,3]
Find middle node, split in half, reverse 2nd half of list, merge
Time: O(n)
Space: O(1)
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if (head->next == NULL) {
return;
}
ListNode* prev = NULL;
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
prev->next = NULL;
ListNode* l1 = head;
ListNode* l2 = reverse(slow);
merge(l1, l2);
}
private:
ListNode* reverse(ListNode* head) {
ListNode* prev = NULL;
ListNode* curr = head;
ListNode* next = curr->next;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void merge(ListNode* l1, ListNode* l2) {
while (l1 != NULL) {
ListNode* p1 = l1->next;
ListNode* p2 = l2->next;
l1->next = l2;
if (p1 == NULL) {
break;
}
l2->next = p1;
l1 = p1;
l2 = p2;
}
}
};