/
148. Sort List v2.cpp
51 lines (51 loc) · 1.67 KB
/
148. Sort List v2.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* sortList(ListNode* head) {
if (!head || !head->next) return head;
ListNode dummy{3}; dummy.next = head;
size_t segLen = 1;
while (true) {
for (ListNode *last = &dummy; last->next; ) {
ListNode *first = last->next;
ListNode *curr = last;
size_t actualFirstSegLen = 0;
for (size_t i = 0; i < segLen; ++i, curr = curr->next) {
if (!curr->next && last == &dummy) { return dummy.next; }
else if (!curr->next) { break; }
++actualFirstSegLen;
}
ListNode *second = curr->next;
size_t actualSecondSegLen = 0;
for (size_t i = 0; i < segLen; ++i, curr = curr->next) {
if (!curr->next) { break; }
++actualSecondSegLen;
}
ListNode *next = curr->next;
ListNode *firstCurr = first, *secondCurr = second;
size_t const combinedLen = actualFirstSegLen + actualSecondSegLen;
for (size_t i = 0; i < combinedLen; ++i) {
int const v1 = firstCurr != second ? firstCurr->val : INT_MAX;
int const v2 = secondCurr != next ? secondCurr->val : INT_MAX;
if (v1 < v2) {
last = last->next = firstCurr;
firstCurr = firstCurr->next;
}
else {
last = last->next = secondCurr;
secondCurr = secondCurr->next;
}
}
last->next = next;
}
segLen *= 2;
}
}
};