Skip to content

Commit a336f65

Browse files
authored
feat: add solutions to lc problem: No.0148 (doocs#3904)
No.0148.Sort List
1 parent 1e21654 commit a336f65

File tree

9 files changed

+273
-263
lines changed

9 files changed

+273
-263
lines changed

solution/0100-0199/0148.Sort List/README.md

+93-87
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:归并排序
71+
72+
我们可以用归并排序的思想来解决。
73+
74+
首先,我们利用快慢指针找到链表的中点,将链表从中点处断开,形成两个独立的子链表 $\textit{l1}$ 和 $\textit{l2}$。
75+
76+
然后,我们递归地对 $\textit{l1}$ 和 $\textit{l2}$ 进行排序,最后将 $\textit{l1}$ 和 $\textit{l2}$ 合并为一个有序链表。
77+
78+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是链表的长度。
7179

7280
<!-- tabs:start -->
7381

@@ -80,26 +88,27 @@ tags:
8088
# self.val = val
8189
# self.next = next
8290
class Solution:
83-
def sortList(self, head: ListNode) -> ListNode:
91+
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8492
if head is None or head.next is None:
8593
return head
8694
slow, fast = head, head.next
8795
while fast and fast.next:
88-
slow, fast = slow.next, fast.next.next
89-
t = slow.next
96+
slow = slow.next
97+
fast = fast.next.next
98+
l1, l2 = head, slow.next
9099
slow.next = None
91-
l1, l2 = self.sortList(head), self.sortList(t)
100+
l1, l2 = self.sortList(l1), self.sortList(l2)
92101
dummy = ListNode()
93-
cur = dummy
102+
tail = dummy
94103
while l1 and l2:
95104
if l1.val <= l2.val:
96-
cur.next = l1
105+
tail.next = l1
97106
l1 = l1.next
98107
else:
99-
cur.next = l2
108+
tail.next = l2
100109
l2 = l2.next
101-
cur = cur.next
102-
cur.next = l1 or l2
110+
tail = tail.next
111+
tail.next = l1 or l2
103112
return dummy.next
104113
```
105114

@@ -126,23 +135,23 @@ class Solution {
126135
slow = slow.next;
127136
fast = fast.next.next;
128137
}
129-
ListNode t = slow.next;
138+
ListNode l1 = head, l2 = slow.next;
130139
slow.next = null;
131-
ListNode l1 = sortList(head);
132-
ListNode l2 = sortList(t);
140+
l1 = sortList(l1);
141+
l2 = sortList(l2);
133142
ListNode dummy = new ListNode();
134-
ListNode cur = dummy;
143+
ListNode tail = dummy;
135144
while (l1 != null && l2 != null) {
136145
if (l1.val <= l2.val) {
137-
cur.next = l1;
146+
tail.next = l1;
138147
l1 = l1.next;
139148
} else {
140-
cur.next = l2;
149+
tail.next = l2;
141150
l2 = l2.next;
142151
}
143-
cur = cur.next;
152+
tail = tail.next;
144153
}
145-
cur.next = l1 == null ? l2 : l1;
154+
tail.next = l1 != null ? l1 : l2;
146155
return dummy.next;
147156
}
148157
}
@@ -164,30 +173,33 @@ class Solution {
164173
class Solution {
165174
public:
166175
ListNode* sortList(ListNode* head) {
167-
if (!head || !head->next) return head;
168-
auto* slow = head;
169-
auto* fast = head->next;
176+
if (!head || !head->next) {
177+
return head;
178+
}
179+
ListNode* slow = head;
180+
ListNode* fast = head->next;
170181
while (fast && fast->next) {
171182
slow = slow->next;
172183
fast = fast->next->next;
173184
}
174-
auto* t = slow->next;
185+
ListNode* l1 = head;
186+
ListNode* l2 = slow->next;
175187
slow->next = nullptr;
176-
auto* l1 = sortList(head);
177-
auto* l2 = sortList(t);
178-
auto* dummy = new ListNode();
179-
auto* cur = dummy;
188+
l1 = sortList(l1);
189+
l2 = sortList(l2);
190+
ListNode* dummy = new ListNode();
191+
ListNode* tail = dummy;
180192
while (l1 && l2) {
181193
if (l1->val <= l2->val) {
182-
cur->next = l1;
194+
tail->next = l1;
183195
l1 = l1->next;
184196
} else {
185-
cur->next = l2;
197+
tail->next = l2;
186198
l2 = l2->next;
187199
}
188-
cur = cur->next;
200+
tail = tail->next;
189201
}
190-
cur->next = l1 ? l1 : l2;
202+
tail->next = l1 ? l1 : l2;
191203
return dummy->next;
192204
}
193205
};
@@ -211,25 +223,27 @@ func sortList(head *ListNode) *ListNode {
211223
for fast != nil && fast.Next != nil {
212224
slow, fast = slow.Next, fast.Next.Next
213225
}
214-
t := slow.Next
226+
l1 := head
227+
l2 := slow.Next
215228
slow.Next = nil
216-
l1, l2 := sortList(head), sortList(t)
229+
l1 = sortList(l1)
230+
l2 = sortList(l2)
217231
dummy := &ListNode{}
218-
cur := dummy
232+
tail := dummy
219233
for l1 != nil && l2 != nil {
220234
if l1.Val <= l2.Val {
221-
cur.Next = l1
235+
tail.Next = l1
222236
l1 = l1.Next
223237
} else {
224-
cur.Next = l2
238+
tail.Next = l2
225239
l2 = l2.Next
226240
}
227-
cur = cur.Next
241+
tail = tail.Next
228242
}
229243
if l1 != nil {
230-
cur.Next = l1
244+
tail.Next = l1
231245
} else {
232-
cur.Next = l2
246+
tail.Next = l2
233247
}
234248
return dummy.Next
235249
}
@@ -251,32 +265,31 @@ func sortList(head *ListNode) *ListNode {
251265
*/
252266

253267
function sortList(head: ListNode | null): ListNode | null {
254-
if (head == null || head.next == null) return head;
255-
// 快慢指针定位中点
256-
let slow: ListNode = head,
257-
fast: ListNode = head.next;
258-
while (fast != null && fast.next != null) {
259-
slow = slow.next;
268+
if (head === null || head.next === null) {
269+
return head;
270+
}
271+
let [slow, fast] = [head, head.next];
272+
while (fast !== null && fast.next !== null) {
273+
slow = slow.next!;
260274
fast = fast.next.next;
261275
}
262-
// 归并排序
263-
let mid: ListNode = slow.next;
276+
let [l1, l2] = [head, slow.next];
264277
slow.next = null;
265-
let l1: ListNode = sortList(head);
266-
let l2: ListNode = sortList(mid);
267-
let dummy: ListNode = new ListNode();
268-
let cur: ListNode = dummy;
269-
while (l1 != null && l2 != null) {
278+
l1 = sortList(l1);
279+
l2 = sortList(l2);
280+
const dummy = new ListNode();
281+
let tail = dummy;
282+
while (l1 !== null && l2 !== null) {
270283
if (l1.val <= l2.val) {
271-
cur.next = l1;
284+
tail.next = l1;
272285
l1 = l1.next;
273286
} else {
274-
cur.next = l2;
287+
tail.next = l2;
275288
l2 = l2.next;
276289
}
277-
cur = cur.next;
290+
tail = tail.next;
278291
}
279-
cur.next = l1 == null ? l2 : l1;
292+
tail.next = l1 ?? l2;
280293
return dummy.next;
281294
}
282295
```
@@ -357,32 +370,31 @@ impl Solution {
357370
* @return {ListNode}
358371
*/
359372
var sortList = function (head) {
360-
if (!head || !head.next) {
373+
if (head === null || head.next === null) {
361374
return head;
362375
}
363-
let slow = head;
364-
let fast = head.next;
365-
while (fast && fast.next) {
376+
let [slow, fast] = [head, head.next];
377+
while (fast !== null && fast.next !== null) {
366378
slow = slow.next;
367379
fast = fast.next.next;
368380
}
369-
let t = slow.next;
381+
let [l1, l2] = [head, slow.next];
370382
slow.next = null;
371-
let l1 = sortList(head);
372-
let l2 = sortList(t);
383+
l1 = sortList(l1);
384+
l2 = sortList(l2);
373385
const dummy = new ListNode();
374-
let cur = dummy;
375-
while (l1 && l2) {
386+
let tail = dummy;
387+
while (l1 !== null && l2 !== null) {
376388
if (l1.val <= l2.val) {
377-
cur.next = l1;
389+
tail.next = l1;
378390
l1 = l1.next;
379391
} else {
380-
cur.next = l2;
392+
tail.next = l2;
381393
l2 = l2.next;
382394
}
383-
cur = cur.next;
395+
tail = tail.next;
384396
}
385-
cur.next = l1 || l2;
397+
tail.next = l1 ?? l2;
386398
return dummy.next;
387399
};
388400
```
@@ -403,37 +415,31 @@ var sortList = function (head) {
403415
*/
404416
public class Solution {
405417
public ListNode SortList(ListNode head) {
406-
if (head == null || head.next == null)
407-
{
418+
if (head == null || head.next == null) {
408419
return head;
409420
}
410421
ListNode slow = head, fast = head.next;
411-
while (fast != null && fast.next != null)
412-
{
422+
while (fast != null && fast.next != null) {
413423
slow = slow.next;
414424
fast = fast.next.next;
415425
}
416-
ListNode t = slow.next;
426+
ListNode l1 = head, l2 = slow.next;
417427
slow.next = null;
418-
ListNode l1 = SortList(head);
419-
ListNode l2 = SortList(t);
428+
l1 = SortList(l1);
429+
l2 = SortList(l2);
420430
ListNode dummy = new ListNode();
421-
ListNode cur = dummy;
422-
while (l1 != null && l2 != null)
423-
{
424-
if (l1.val <= l2.val)
425-
{
426-
cur.next = l1;
431+
ListNode tail = dummy;
432+
while (l1 != null && l2 != null) {
433+
if (l1.val <= l2.val) {
434+
tail.next = l1;
427435
l1 = l1.next;
428-
}
429-
else
430-
{
431-
cur.next = l2;
436+
} else {
437+
tail.next = l2;
432438
l2 = l2.next;
433439
}
434-
cur = cur.next;
440+
tail = tail.next;
435441
}
436-
cur.next = l1 == null ? l2 : l1;
442+
tail.next = l1 != null ? l1 : l2;
437443
return dummy.next;
438444
}
439445
}

0 commit comments

Comments
 (0)