-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorder List.cpp
More file actions
73 lines (68 loc) · 1.78 KB
/
Reorder List.cpp
File metadata and controls
73 lines (68 loc) · 1.78 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
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Straight-forward solution, O(3 * n)
class Solution {
public:
void reorderList(ListNode *head) {
int n = getLength(head);
if (n < 2) return;
if (n % 2 != 0) n += 1;
int m = n / 2;
ListNode *l = head;
ListNode *h = head->next;
while (--m) {
l = l->next;
h = h->next;
}
l->next = NULL;
h = reverseList(h);
interleaveLists(head, h);
}
private:
ListNode *reverseList(ListNode *head) {
if (!head) return head;
ListNode dummy(0);
ListNode *dh = &dummy;
dh->next = head;
ListNode *last = head;
ListNode *cur = head->next;
while (cur) {
last->next = cur->next;
cur->next = dh->next;
dh->next = cur;
cur = last->next;
}
return dh->next;
}
void interleaveLists(ListNode *head1, ListNode *head2) {
ListNode dummy(0);
ListNode *dh = &dummy;
ListNode *cur = dh;
while (head1 && head2) {
cur->next = head1;
head1 = head1->next;
cur = cur->next;
cur->next = head2;
head2 = head2->next;
cur = cur->next;
}
while (head1) {cur->next = head1; head1 = head1->next; cur = cur->next;}
while (head2) {cur->next = head2; head1 = head2->next; cur = cur->next;}
cur->next = NULL;
}
int getLength(ListNode *head) {
int len = 0;
while (head) {
len ++;
head = head->next;
}
return len;
}
};
// Tricky solution, O(n)