-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0148. Sort List.cpp
More file actions
61 lines (56 loc) · 1.6 KB
/
0148. Sort List.cpp
File metadata and controls
61 lines (56 loc) · 1.6 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
/**
* 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:
// helper method to merge
ListNode *merge(ListNode *one, ListNode *two) {
// create current and temp
ListNode *curr = new ListNode(0);
ListNode *temp = curr;
// merge them together
while (one != NULL && two != NULL) {
// put 1 first
if (one->val < two->val) {
curr->next = one;
one = one->next;
}
// otherwise put 2
else {
curr->next = two;
two = two->next;
}
curr = curr->next;
}
// any remaining and return
if (one != NULL)
curr->next = one;
if (two != NULL)
curr->next = two;
return temp->next;
}
// merge sort
ListNode *sortList(ListNode *head) {
// edge case: 0 or 1 node
if (head == NULL || head->next == NULL)
return head;
// slow and fast
ListNode *slow = head;
ListNode *fast = head->next;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
// divide into first and second half, recurse through
fast = slow->next;
slow->next = NULL;
return merge(sortList(head), sortList(fast));
}
};