-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort List.cpp
More file actions
194 lines (173 loc) · 5.52 KB
/
Sort List.cpp
File metadata and controls
194 lines (173 loc) · 5.52 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// bubble sort, O(n^2) TLE, stupid method,
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (!head) return head;
if (!head->next) return head;
ListNode dummy(0);
ListNode *dh = &dummy;
dh->next = head;
ListNode *end = NULL;
while (dh->next != end) {
ListNode *last = dh;
ListNode *cur = dh->next;
while (cur != end) {
if (cur->next) {
if (cur->val > cur->next->val) {
last->next = cur->next;
cur->next = cur->next->next;
last->next->next = cur;
last = last->next;
continue;
}
}
last = last->next;
cur = cur->next;
}
end = last;
}
return dh->next;
}
};
// merge sort, which borrow some incredible trick from internet. A merge solution is easy to come up with.
// Since, you know, random access is not available for linked list. You have to do something to get value of specific node.
// Luckily, merge sort have its spontaneous feature that corresponding pointer pointing to element in need of sorting moves one by one from left to right during each recursion process.
// That means we can add some trick in the recursion function making use this feature by changing the pointer value assignment to pointer reference assignment.
// And keep manipulating the reference in every recursion so as to move the head pointer forward.
class Solution {
public:
ListNode *sortList(ListNode *head) {
return sortLinkedList(head, getLength(head));
}
ListNode *sortLinkedList(ListNode *&head, int n) {
if (n == 0) return head;
if (n == 1) {
ListNode *cur = head;
head = head->next;
cur->next = NULL;
return cur;
}
int mid = n / 2;
ListNode *head1 = sortLinkedList(head, mid);
ListNode *head2 = sortLinkedList(head, n - mid);
return mergeLinkedList(head1, head2);
}
ListNode *mergeLinkedList(ListNode *head1, ListNode *head2) {
ListNode dummy(0);
ListNode *dh = &dummy;
ListNode *cur = dh;
while (head1 && head2) {
if (head1->val > head2->val) {
cur->next = head2;
head2 = head2->next;
} else {
cur->next = head1;
head1 = head1->next;
}
cur = cur->next;
}
while (head1) {cur->next = head1; head1 = head1->next; cur = cur->next;}
while (head2) {cur->next = head2; head2 = head2->next; cur = cur->next;}
cur->next = NULL;
return dh->next;
}
int getLength(ListNode *head) {
int ret = 0;
while (head) {
ret ++;
head = head->next;
}
return ret;
}
};
// O(2 * n * logn)
// shorter but slower
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (!head || !head->next) return head;
ListNode *l = head, *h = head, *t = NULL;
while (h && h->next) {
h = h->next->next;
t = l;
l = l->next;
}
t->next = NULL;
ListNode *head2 = l;
head = sortList(head);
head2 = sortList(head2);
// merge two lists
ListNode dh(0);
ListNode *cur = &dh;
while (head && head2) {
if (head->val > head2->val) {
cur->next = head2;
head2 = head2->next;
} else {
cur->next = head;
head = head->next;
}
cur = cur->next;
}
if (head) cur->next = head;
if (head2) cur->next = head2;
return dh.next;
}
};
// n * O(logn)
class Solution {
public:
ListNode* sortList(ListNode* head) {
int len = 0;
for (ListNode* p = head; p != nullptr; p = p->next) len ++;
if (len < 2) return head;
ListNode dh(0);
dh.next = head;
int skip = 1;
while (skip < len) {
ListNode* pre = &dh;
ListNode* h1 = pre->next;
ListNode* h2 = h1;
while (h2) {
int s = 0;
while (h2 && (++s) <= skip) h2 = h2->next;
ListNode* end = h2;
s = 0;
while (end && (++s) <= skip) end = end->next;
pre = merge(pre, h1, h2, end);
h1 = pre->next;
h2 = h1;
}
skip *= 2;
}
return dh.next;
}
void print(ListNode* head) {
ListNode* p = head;
while (p) {
cout << p->val << "->";
p = p->next;
}
cout << "null" << endl;
}
ListNode* merge(ListNode* pre, ListNode* h1, ListNode* h2, ListNode* end) {
ListNode* pre1 = pre;
ListNode* p1 = h1;
ListNode* pre2 = h1;
while (pre2->next != h2) pre2 = pre2->next;
ListNode* p2 = h2;
while (p1 != p2 && p2 != end) {
if (p1->val < p2->val) {
p1 = p1->next;
} else {
pre2->next = p2->next;
p2->next = p1;
pre1->next = p2;
p2 = pre2->next;
}
pre1 = pre1->next;
}
while (pre2->next != end) pre2 = pre2->next;
return pre2;
}
};