-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path148_Sort List.cpp
More file actions
56 lines (50 loc) · 1.47 KB
/
148_Sort List.cpp
File metadata and controls
56 lines (50 loc) · 1.47 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
#include <iostream>
using namespace std;
// 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;
// find the mid node
ListNode *slow = head, *fast = head;
while (fast->next && fast->next->next) {
fast = fast->next->next;
slow = slow->next;
}
fast = slow->next; slow->next = NULL;
// sort half lists seperately
ListNode *l1 = sortList(head);
ListNode *l2 = sortList(fast);
// merge two sorted lists
return merge(l1, l2);
}
ListNode* merge(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* tail = dummy;
while (l1 && l2) {
if (l1->val <= l2->val) {
tail->next = l1; l1 = l1->next; tail = tail->next;
} else {
tail->next = l2; l2 = l2->next; tail = tail->next;
}
}
if (l1) tail->next = l1;
if (l2) tail->next = l2;
return dummy->next;
}
};
int main() {
ListNode *n1 = new ListNode(5), *n2 = new ListNode(2), *n3 = new ListNode(6), *n4 = new ListNode(1);
n1->next = n2; n2->next = n3; n3->next = n4;
Solution s;
ListNode* head = s.sortList(n1);
while (head) {
cout << head->val << " "; head = head->next;
}
return 0;
}