67
67
68
68
<!-- solution:start -->
69
69
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$ 是链表的长度。
71
79
72
80
<!-- tabs:start -->
73
81
@@ -80,26 +88,27 @@ tags:
80
88
# self.val = val
81
89
# self.next = next
82
90
class Solution :
83
- def sortList (self , head : ListNode) -> ListNode:
91
+ def sortList (self , head : Optional[ ListNode] ) -> Optional[ ListNode] :
84
92
if head is None or head.next is None :
85
93
return head
86
94
slow, fast = head, head.next
87
95
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
90
99
slow.next = None
91
- l1, l2 = self .sortList(head ), self .sortList(t )
100
+ l1, l2 = self .sortList(l1 ), self .sortList(l2 )
92
101
dummy = ListNode()
93
- cur = dummy
102
+ tail = dummy
94
103
while l1 and l2:
95
104
if l1.val <= l2.val:
96
- cur .next = l1
105
+ tail .next = l1
97
106
l1 = l1.next
98
107
else :
99
- cur .next = l2
108
+ tail .next = l2
100
109
l2 = l2.next
101
- cur = cur .next
102
- cur .next = l1 or l2
110
+ tail = tail .next
111
+ tail .next = l1 or l2
103
112
return dummy.next
104
113
```
105
114
@@ -126,23 +135,23 @@ class Solution {
126
135
slow = slow. next;
127
136
fast = fast. next. next;
128
137
}
129
- ListNode t = slow. next;
138
+ ListNode l1 = head, l2 = slow. next;
130
139
slow. next = null ;
131
- ListNode l1 = sortList(head );
132
- ListNode l2 = sortList(t );
140
+ l1 = sortList(l1 );
141
+ l2 = sortList(l2 );
133
142
ListNode dummy = new ListNode ();
134
- ListNode cur = dummy;
143
+ ListNode tail = dummy;
135
144
while (l1 != null && l2 != null ) {
136
145
if (l1. val <= l2. val) {
137
- cur . next = l1;
146
+ tail . next = l1;
138
147
l1 = l1. next;
139
148
} else {
140
- cur . next = l2;
149
+ tail . next = l2;
141
150
l2 = l2. next;
142
151
}
143
- cur = cur . next;
152
+ tail = tail . next;
144
153
}
145
- cur . next = l1 == null ? l2 : l1 ;
154
+ tail . next = l1 != null ? l1 : l2 ;
146
155
return dummy. next;
147
156
}
148
157
}
@@ -164,30 +173,33 @@ class Solution {
164
173
class Solution {
165
174
public:
166
175
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;
170
181
while (fast && fast->next) {
171
182
slow = slow->next;
172
183
fast = fast->next->next;
173
184
}
174
- auto* t = slow->next;
185
+ ListNode* l1 = head;
186
+ ListNode* l2 = slow->next;
175
187
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;
180
192
while (l1 && l2) {
181
193
if (l1->val <= l2->val) {
182
- cur ->next = l1;
194
+ tail ->next = l1;
183
195
l1 = l1->next;
184
196
} else {
185
- cur ->next = l2;
197
+ tail ->next = l2;
186
198
l2 = l2->next;
187
199
}
188
- cur = cur ->next;
200
+ tail = tail ->next;
189
201
}
190
- cur ->next = l1 ? l1 : l2;
202
+ tail ->next = l1 ? l1 : l2;
191
203
return dummy->next;
192
204
}
193
205
};
@@ -211,25 +223,27 @@ func sortList(head *ListNode) *ListNode {
211
223
for fast != nil && fast.Next != nil {
212
224
slow, fast = slow.Next, fast.Next.Next
213
225
}
214
- t := slow.Next
226
+ l1 := head
227
+ l2 := slow.Next
215
228
slow.Next = nil
216
- l1, l2 := sortList(head), sortList(t)
229
+ l1 = sortList(l1)
230
+ l2 = sortList(l2)
217
231
dummy := &ListNode{}
218
- cur := dummy
232
+ tail := dummy
219
233
for l1 != nil && l2 != nil {
220
234
if l1.Val <= l2.Val {
221
- cur .Next = l1
235
+ tail .Next = l1
222
236
l1 = l1.Next
223
237
} else {
224
- cur .Next = l2
238
+ tail .Next = l2
225
239
l2 = l2.Next
226
240
}
227
- cur = cur .Next
241
+ tail = tail .Next
228
242
}
229
243
if l1 != nil {
230
- cur .Next = l1
244
+ tail .Next = l1
231
245
} else {
232
- cur .Next = l2
246
+ tail .Next = l2
233
247
}
234
248
return dummy.Next
235
249
}
@@ -251,32 +265,31 @@ func sortList(head *ListNode) *ListNode {
251
265
*/
252
266
253
267
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 ! ;
260
274
fast = fast .next .next ;
261
275
}
262
- // 归并排序
263
- let mid: ListNode = slow .next ;
276
+ let [l1, l2] = [head , slow .next ];
264
277
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 ) {
270
283
if (l1 .val <= l2 .val ) {
271
- cur .next = l1 ;
284
+ tail .next = l1 ;
272
285
l1 = l1 .next ;
273
286
} else {
274
- cur .next = l2 ;
287
+ tail .next = l2 ;
275
288
l2 = l2 .next ;
276
289
}
277
- cur = cur .next ;
290
+ tail = tail .next ;
278
291
}
279
- cur .next = l1 == null ? l2 : l1 ;
292
+ tail .next = l1 ?? l2 ;
280
293
return dummy .next ;
281
294
}
282
295
```
@@ -357,32 +370,31 @@ impl Solution {
357
370
* @return {ListNode}
358
371
*/
359
372
var sortList = function (head ) {
360
- if (! head || ! head .next ) {
373
+ if (head === null || head .next === null ) {
361
374
return head;
362
375
}
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 ) {
366
378
slow = slow .next ;
367
379
fast = fast .next .next ;
368
380
}
369
- let t = slow .next ;
381
+ let [l1, l2] = [head, slow .next ] ;
370
382
slow .next = null ;
371
- let l1 = sortList (head );
372
- let l2 = sortList (t );
383
+ l1 = sortList (l1 );
384
+ l2 = sortList (l2 );
373
385
const dummy = new ListNode ();
374
- let cur = dummy;
375
- while (l1 && l2) {
386
+ let tail = dummy;
387
+ while (l1 !== null && l2 !== null ) {
376
388
if (l1 .val <= l2 .val ) {
377
- cur .next = l1;
389
+ tail .next = l1;
378
390
l1 = l1 .next ;
379
391
} else {
380
- cur .next = l2;
392
+ tail .next = l2;
381
393
l2 = l2 .next ;
382
394
}
383
- cur = cur .next ;
395
+ tail = tail .next ;
384
396
}
385
- cur .next = l1 || l2;
397
+ tail .next = l1 ?? l2;
386
398
return dummy .next ;
387
399
};
388
400
` ` `
@@ -403,37 +415,31 @@ var sortList = function (head) {
403
415
*/
404
416
public class Solution {
405
417
public ListNode SortList (ListNode head ) {
406
- if (head == null || head .next == null )
407
- {
418
+ if (head == null || head .next == null ) {
408
419
return head;
409
420
}
410
421
ListNode slow = head, fast = head .next ;
411
- while (fast != null && fast .next != null )
412
- {
422
+ while (fast != null && fast .next != null ) {
413
423
slow = slow .next ;
414
424
fast = fast .next .next ;
415
425
}
416
- ListNode t = slow .next ;
426
+ ListNode l1 = head, l2 = slow .next ;
417
427
slow .next = null ;
418
- ListNode l1 = SortList (head );
419
- ListNode l2 = SortList (t );
428
+ l1 = SortList (l1 );
429
+ l2 = SortList (l2 );
420
430
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;
427
435
l1 = l1 .next ;
428
- }
429
- else
430
- {
431
- cur .next = l2 ;
436
+ } else {
437
+ tail .next = l2;
432
438
l2 = l2 .next ;
433
439
}
434
- cur = cur .next ;
440
+ tail = tail .next ;
435
441
}
436
- cur .next = l1 == null ? l2 : l1 ;
442
+ tail .next = l1 != null ? l1 : l2 ;
437
443
return dummy .next ;
438
444
}
439
445
}
0 commit comments