-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148.cc
118 lines (113 loc) · 3.14 KB
/
148.cc
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* 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 == nullptr) {
// return head;
// }
// ListNode* tail = head;
// for (; tail->next; tail = tail->next);
// quickSort(head, tail);
// return head;
// }
// void quickSort(ListNode* head, ListNode* tail) {
// if (head == tail) {
// return;
// }
// if (head->next == tail) {
// if (head->val > tail->val) {
// swap(head->val, tail->val);
// }
// return;
// }
// selectPrior(head, tail);
// auto pos = partition(head, tail);
// quickSort(head, pos);
// quickSort(pos->next, tail);
// }
// ListNode* partition(ListNode* head, ListNode* tail) {
// auto pre = head;
// for (auto cur = head; cur != tail; cur = cur->next) {
// if (cur->val < tail->val) {
// swap(pre->val, cur->val);
// pre = pre->next;
// }
// }
// swap(pre->val, tail->val);
// return pre;
// }
// void selectPrior(ListNode* head, ListNode* tail) {
// auto three = head->next;
// if (head->val > three->val) {
// if (three->val > tail->val) {
// swap(three->val, tail->val);
// } else if (tail->val > head->val) {
// swap(tail->val, head->val);
// }
// } else {
// if (head->val > tail->val) {
// swap(head->val, tail->val);
// } else if (tail->val > three->val) {
// swap(tail->val, three->val);
// }
// }
// }
// };
class Solution {
public:
ListNode* sortList(ListNode* head) {
if (head == nullptr || head->next == nullptr) {
return head;
}
auto slow = head;
auto fast = head->next;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
auto mid = slow->next;
slow->next = nullptr;
auto left = sortList(head);
auto right = sortList(mid);
ListNode* new_head;
if (left->val < right->val) {
new_head = left;
left = left->next;
} else {
new_head = right;
right = right->next;
}
auto tail = new_head;
while (left && right) {
if (left->val < right->val) {
tail->next = left;
left = left->next;
} else {
tail->next = right;
right = right->next;
}
tail = tail->next;
}
if (left) {
tail->next = left;
} else {
tail->next = right;
}
return new_head;
}
};