-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path148-Sort-List.cpp
More file actions
55 lines (44 loc) · 1.43 KB
/
148-Sort-List.cpp
File metadata and controls
55 lines (44 loc) · 1.43 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
class Solution {
public:
ListNode* sortList(ListNode* head) {
// Split list recursion
// Base case
if(!head || !head->next){
return head;
}
ListNode* fast = head;
ListNode* slow = head;
ListNode* split = head;
while(fast && fast->next){
split = slow;
slow = slow->next;
fast = fast->next->next;
}
split->next = nullptr; // This splits the list
ListNode* left = sortList(head);
ListNode* right = sortList(slow);
return merge(left, right);
}
ListNode* merge(ListNode* left, ListNode* right){
// Base case for both left and right
if(!left) return right;
if(!right) return left;
if(left->val < right->val){
left->next = merge(left->next, right);
return left;
}
else{
right->next = merge(left, right->next);
return right;
}
}
};
/* 148. Sort-List.cpp
//////////////////////////////////////////////////
Given the head of a linked list, return the list after sorting it in ascending order.
Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?
Input: head = [4,2,1,3]
Output: [1,2,3,4]
https://leetcode.com/problems/sort-list/
//////////////////////////////////////////////////
*/