-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_148.cpp
More file actions
162 lines (137 loc) · 3.12 KB
/
Leetcode_148.cpp
File metadata and controls
162 lines (137 loc) · 3.12 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 148. Sort List
// https://leetcode.com/problems/sort-list/
/*
Given the head of a linked list, return the list after sorting it in ascending order.
Example 1:
Input: head = [4,2,1,3]
Output: [1,2,3,4]
Example 2:
Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]
Example 3:
Input: head = []
Output: []
Constraints:
The number of nodes in the list is in the range [0, 5 * 104].
-105 <= Node.val <= 105
Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?
*/
#include <iostream>
using namespace std;
// 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:
// merge sort
// time complexity: O(n log n)
// space complexity: O(log n)
ListNode *mergeSort(ListNode *head)
{
if (!head || !head->next)
return head;
ListNode *fast = head;
ListNode *slow = head;
ListNode *beforeCut = nullptr;
while (fast && fast->next)
{
beforeCut = slow;
fast = fast->next->next;
slow = slow->next;
}
if (fast)
{
beforeCut = slow;
slow = slow->next;
}
beforeCut->next = nullptr;
ListNode *firstHalf = mergeSort(head);
ListNode *secondHalf = mergeSort(slow);
ListNode dummy(0);
ListNode *tail = &dummy;
while (firstHalf && secondHalf)
{
if (firstHalf->val < secondHalf->val)
{
tail->next = firstHalf;
tail = firstHalf;
firstHalf = firstHalf->next;
}
else
{
tail->next = secondHalf;
tail = secondHalf;
secondHalf = secondHalf->next;
}
}
tail->next = firstHalf ? firstHalf : secondHalf;
return dummy.next;
}
// time complexity: O(n log n)
// space complexity: O(log n)
ListNode *sortList(ListNode *head)
{
return mergeSort(head);
}
};
int main()
{
Solution solution;
ListNode *head = new ListNode(4, new ListNode(2, new ListNode(1, new ListNode(3))));
cout << "Original list: ";
ListNode *temp = head;
while (temp != nullptr)
{
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
ListNode *sortedHead = solution.sortList(head);
cout << "Sorted list: ";
while (sortedHead != nullptr)
{
cout << sortedHead->val << " ";
sortedHead = sortedHead->next;
}
cout << endl;
// clear the memory
while (head != nullptr)
{
ListNode *temp = head;
head = head->next;
delete temp;
}
ListNode *head2 = new ListNode(-1, new ListNode(5, new ListNode(3, new ListNode(4, new ListNode(0)))));
cout << "Original list: ";
temp = head2;
while (temp != nullptr)
{
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
sortedHead = solution.sortList(head2);
cout << "Sorted list: ";
temp = sortedHead;
while (temp != nullptr)
{
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
// clear the memory
while (head2 != nullptr)
{
ListNode *temp = head2;
head2 = head2->next;
delete temp;
}
return 0;
}