@@ -45,7 +45,11 @@ L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub>
45
45
46
46
## Solutions
47
47
48
- ### Solution 1
48
+ ### Solution 1: Fast and Slow Pointers + Reverse List + Merge Lists
49
+
50
+ We first use fast and slow pointers to find the midpoint of the linked list, then reverse the second half of the list, and finally merge the two halves.
51
+
52
+ The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
49
53
50
54
<!-- tabs:start -->
51
55
@@ -57,26 +61,21 @@ L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub>
57
61
# self.next = next
58
62
class Solution :
59
63
def reorderList (self , head : Optional[ListNode]) -> None :
60
- # 快慢指针找到链表中点
61
64
fast = slow = head
62
65
while fast.next and fast.next.next:
63
66
slow = slow.next
64
67
fast = fast.next.next
65
68
66
- # cur 指向右半部分链表
67
69
cur = slow.next
68
70
slow.next = None
69
71
70
- # 反转右半部分链表
71
72
pre = None
72
73
while cur:
73
74
t = cur.next
74
75
cur.next = pre
75
76
pre, cur = cur, t
76
77
cur = head
77
78
78
- # 此时 cur, pre 分别指向链表左右两半的第一个节点
79
- # 合并
80
79
while pre:
81
80
t = pre.next
82
81
pre.next = cur.next
@@ -97,18 +96,15 @@ class Solution:
97
96
*/
98
97
class Solution {
99
98
public void reorderList (ListNode head ) {
100
- // 快慢指针找到链表中点
101
99
ListNode fast = head, slow = head;
102
100
while (fast. next != null && fast. next. next != null ) {
103
101
slow = slow. next;
104
102
fast = fast. next. next;
105
103
}
106
104
107
- // cur 指向右半部分链表
108
105
ListNode cur = slow. next;
109
106
slow. next = null ;
110
107
111
- // 反转右半部分链表
112
108
ListNode pre = null ;
113
109
while (cur != null ) {
114
110
ListNode t = cur. next;
@@ -118,8 +114,6 @@ class Solution {
118
114
}
119
115
cur = head;
120
116
121
- // 此时 cur, pre 分别指向链表左右两半的第一个节点
122
- // 合并
123
117
while (pre != null ) {
124
118
ListNode t = pre. next;
125
119
pre. next = cur. next;
@@ -145,19 +139,16 @@ class Solution {
145
139
class Solution {
146
140
public:
147
141
void reorderList(ListNode* head) {
148
- // 快慢指针找到链表中点
149
142
ListNode* fast = head;
150
143
ListNode* slow = head;
151
144
while (fast->next && fast->next->next) {
152
145
slow = slow->next;
153
146
fast = fast->next->next;
154
147
}
155
148
156
- // cur 指向右半部分链表
157
149
ListNode* cur = slow->next;
158
150
slow->next = nullptr;
159
151
160
- // 反转右半部分链表
161
152
ListNode* pre = nullptr;
162
153
while (cur) {
163
154
ListNode* t = cur->next;
@@ -167,8 +158,6 @@ public:
167
158
}
168
159
cur = head;
169
160
170
- // 此时 cur, pre 分别指向链表左右两半的第一个节点
171
- // 合并
172
161
while (pre) {
173
162
ListNode* t = pre->next;
174
163
pre->next = cur->next;
@@ -189,17 +178,14 @@ public:
189
178
* }
190
179
*/
191
180
func reorderList (head *ListNode ) {
192
- // 快慢指针找到链表中点
193
181
fast , slow := head, head
194
182
for fast.Next != nil && fast.Next .Next != nil {
195
183
slow, fast = slow.Next , fast.Next .Next
196
184
}
197
185
198
- // cur 指向右半部分链表
199
186
cur := slow.Next
200
187
slow.Next = nil
201
188
202
- // 反转右半部分链表
203
189
var pre *ListNode
204
190
for cur != nil {
205
191
t := cur.Next
@@ -208,8 +194,6 @@ func reorderList(head *ListNode) {
208
194
}
209
195
cur = head
210
196
211
- // 此时 cur, pre 分别指向链表左右两半的第一个节点
212
- // 合并
213
197
for pre != nil {
214
198
t := pre.Next
215
199
pre.Next = cur.Next
@@ -236,20 +220,27 @@ func reorderList(head *ListNode) {
236
220
Do not return anything, modify head in-place instead.
237
221
*/
238
222
function reorderList(head : ListNode | null ): void {
239
- const arr = [] ;
240
- let node = head ;
241
- while (node . next != null ) {
242
- arr . push ( node ) ;
243
- node = node .next ;
223
+ let slow = head ;
224
+ let fast = head ;
225
+ while (fast && fast . next ) {
226
+ slow = slow . next ;
227
+ fast = fast . next .next ;
244
228
}
245
- let l = 0 ;
246
- let r = arr .length - 1 ;
247
- while (l < r ) {
248
- const start = arr [l ];
249
- const end = arr [r ];
250
- [end .next .next , start .next , end .next ] = [start .next , end .next , null ];
251
- l ++ ;
252
- r -- ;
229
+
230
+ let next = slow .next ;
231
+ slow .next = null ;
232
+ while (next ) {
233
+ [next .next , slow , next ] = [slow , next , next .next ];
234
+ }
235
+
236
+ let left = head ;
237
+ let right = slow ;
238
+ while (right .next ) {
239
+ const next = left .next ;
240
+ left .next = right ;
241
+ right = right .next ;
242
+ left .next .next = next ;
243
+ left = left .next .next ;
253
244
}
254
245
}
255
246
```
@@ -305,19 +296,16 @@ impl Solution {
305
296
* @return {void} Do not return anything, modify head in-place instead.
306
297
*/
307
298
var reorderList = function (head ) {
308
- // 快慢指针找到链表中点
309
299
let slow = head;
310
300
let fast = head;
311
301
while (fast .next && fast .next .next ) {
312
302
slow = slow .next ;
313
303
fast = fast .next .next ;
314
304
}
315
305
316
- // cur 指向右半部分链表
317
306
let cur = slow .next ;
318
307
slow .next = null ;
319
308
320
- // 反转右半部分链表
321
309
let pre = null ;
322
310
while (cur) {
323
311
const t = cur .next ;
@@ -327,8 +315,6 @@ var reorderList = function (head) {
327
315
}
328
316
cur = head;
329
317
330
- // 此时 cur, pre 分别指向链表左右两半的第一个节点
331
- // 合并
332
318
while (pre) {
333
319
const t = pre .next ;
334
320
pre .next = cur .next ;
@@ -353,19 +339,16 @@ var reorderList = function (head) {
353
339
*/
354
340
public class Solution {
355
341
public void ReorderList (ListNode head ) {
356
- // 快慢指针找到链表中点
357
342
ListNode slow = head ;
358
343
ListNode fast = head ;
359
344
while (fast .next != null && fast .next .next != null ) {
360
345
slow = slow .next ;
361
346
fast = fast .next .next ;
362
347
}
363
348
364
- // cur 指向右半部分链表
365
349
ListNode cur = slow .next ;
366
350
slow .next = null ;
367
351
368
- // 反转右半部分链表
369
352
ListNode pre = null ;
370
353
while (cur != null ) {
371
354
ListNode t = cur .next ;
@@ -375,8 +358,6 @@ public class Solution {
375
358
}
376
359
cur = head ;
377
360
378
- // 此时 cur, pre 分别指向链表左右两半的第一个节点
379
- // 合并
380
361
while (pre != null ) {
381
362
ListNode t = pre .next ;
382
363
pre .next = cur .next ;
@@ -390,53 +371,4 @@ public class Solution {
390
371
391
372
<!-- tabs:end -->
392
373
393
- ### Solution 2
394
-
395
- <!-- tabs:start -->
396
-
397
- ``` ts
398
- /**
399
- * Definition for singly-linked list.
400
- * class ListNode {
401
- * val: number
402
- * next: ListNode | null
403
- * constructor(val?: number, next?: ListNode | null) {
404
- * this.val = (val===undefined ? 0 : val)
405
- * this.next = (next===undefined ? null : next)
406
- * }
407
- * }
408
- */
409
-
410
- /**
411
- Do not return anything, modify head in-place instead.
412
- */
413
- function reorderList(head : ListNode | null ): void {
414
- let slow = head ;
415
- let fast = head ;
416
- // 找到中心节点
417
- while (fast != null && fast .next != null ) {
418
- slow = slow .next ;
419
- fast = fast .next .next ;
420
- }
421
- // 反转节点
422
- let next = slow .next ;
423
- slow .next = null ;
424
- while (next != null ) {
425
- [next .next , slow , next ] = [slow , next , next .next ];
426
- }
427
- // 合并
428
- let left = head ;
429
- let right = slow ;
430
- while (right .next != null ) {
431
- const next = left .next ;
432
- left .next = right ;
433
- right = right .next ;
434
- left .next .next = next ;
435
- left = left .next .next ;
436
- }
437
- }
438
- ```
439
-
440
- <!-- tabs:end -->
441
-
442
374
<!-- end -->
0 commit comments