Skip to content

Commit 51d9ecf

Browse files
authored
feat: update solutions to lc problem: No.0143 (#2547)
No.0143.Reorder List
1 parent 59675bb commit 51d9ecf

File tree

10 files changed

+67
-239
lines changed

10 files changed

+67
-239
lines changed

solution/0100-0199/0143.Reorder List/README.md

+21-62
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,28 @@ func reorderList(head *ListNode) {
245245
Do not return anything, modify head in-place instead.
246246
*/
247247
function reorderList(head: ListNode | null): void {
248-
const arr = [];
249-
let node = head;
250-
while (node.next != null) {
251-
arr.push(node);
252-
node = node.next;
248+
let slow = head;
249+
let fast = head;
250+
// 找到中心节点
251+
while (fast && fast.next) {
252+
slow = slow.next;
253+
fast = fast.next.next;
254+
}
255+
// 反转节点
256+
let next = slow.next;
257+
slow.next = null;
258+
while (next) {
259+
[next.next, slow, next] = [slow, next, next.next];
253260
}
254-
let l = 0;
255-
let r = arr.length - 1;
256-
while (l < r) {
257-
const start = arr[l];
258-
const end = arr[r];
259-
[end.next.next, start.next, end.next] = [start.next, end.next, null];
260-
l++;
261-
r--;
261+
// 合并
262+
let left = head;
263+
let right = slow;
264+
while (right.next) {
265+
const next = left.next;
266+
left.next = right;
267+
right = right.next;
268+
left.next.next = next;
269+
left = left.next.next;
262270
}
263271
}
264272
```
@@ -399,53 +407,4 @@ public class Solution {
399407

400408
<!-- tabs:end -->
401409

402-
### 方法二
403-
404-
<!-- tabs:start -->
405-
406-
```ts
407-
/**
408-
* Definition for singly-linked list.
409-
* class ListNode {
410-
* val: number
411-
* next: ListNode | null
412-
* constructor(val?: number, next?: ListNode | null) {
413-
* this.val = (val===undefined ? 0 : val)
414-
* this.next = (next===undefined ? null : next)
415-
* }
416-
* }
417-
*/
418-
419-
/**
420-
Do not return anything, modify head in-place instead.
421-
*/
422-
function reorderList(head: ListNode | null): void {
423-
let slow = head;
424-
let fast = head;
425-
// 找到中心节点
426-
while (fast != null && fast.next != null) {
427-
slow = slow.next;
428-
fast = fast.next.next;
429-
}
430-
// 反转节点
431-
let next = slow.next;
432-
slow.next = null;
433-
while (next != null) {
434-
[next.next, slow, next] = [slow, next, next.next];
435-
}
436-
// 合并
437-
let left = head;
438-
let right = slow;
439-
while (right.next != null) {
440-
const next = left.next;
441-
left.next = right;
442-
right = right.next;
443-
left.next.next = next;
444-
left = left.next.next;
445-
}
446-
}
447-
```
448-
449-
<!-- tabs:end -->
450-
451410
<!-- end -->

solution/0100-0199/0143.Reorder List/README_EN.md

+25-93
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ L<sub>0</sub> &rarr; L<sub>n</sub> &rarr; L<sub>1</sub> &rarr; L<sub>n - 1</sub>
4545

4646
## Solutions
4747

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)$.
4953

5054
<!-- tabs:start -->
5155

@@ -57,26 +61,21 @@ L<sub>0</sub> &rarr; L<sub>n</sub> &rarr; L<sub>1</sub> &rarr; L<sub>n - 1</sub>
5761
# self.next = next
5862
class Solution:
5963
def reorderList(self, head: Optional[ListNode]) -> None:
60-
# 快慢指针找到链表中点
6164
fast = slow = head
6265
while fast.next and fast.next.next:
6366
slow = slow.next
6467
fast = fast.next.next
6568

66-
# cur 指向右半部分链表
6769
cur = slow.next
6870
slow.next = None
6971

70-
# 反转右半部分链表
7172
pre = None
7273
while cur:
7374
t = cur.next
7475
cur.next = pre
7576
pre, cur = cur, t
7677
cur = head
7778

78-
# 此时 cur, pre 分别指向链表左右两半的第一个节点
79-
# 合并
8079
while pre:
8180
t = pre.next
8281
pre.next = cur.next
@@ -97,18 +96,15 @@ class Solution:
9796
*/
9897
class Solution {
9998
public void reorderList(ListNode head) {
100-
// 快慢指针找到链表中点
10199
ListNode fast = head, slow = head;
102100
while (fast.next != null && fast.next.next != null) {
103101
slow = slow.next;
104102
fast = fast.next.next;
105103
}
106104

107-
// cur 指向右半部分链表
108105
ListNode cur = slow.next;
109106
slow.next = null;
110107

111-
// 反转右半部分链表
112108
ListNode pre = null;
113109
while (cur != null) {
114110
ListNode t = cur.next;
@@ -118,8 +114,6 @@ class Solution {
118114
}
119115
cur = head;
120116

121-
// 此时 cur, pre 分别指向链表左右两半的第一个节点
122-
// 合并
123117
while (pre != null) {
124118
ListNode t = pre.next;
125119
pre.next = cur.next;
@@ -145,19 +139,16 @@ class Solution {
145139
class Solution {
146140
public:
147141
void reorderList(ListNode* head) {
148-
// 快慢指针找到链表中点
149142
ListNode* fast = head;
150143
ListNode* slow = head;
151144
while (fast->next && fast->next->next) {
152145
slow = slow->next;
153146
fast = fast->next->next;
154147
}
155148

156-
// cur 指向右半部分链表
157149
ListNode* cur = slow->next;
158150
slow->next = nullptr;
159151

160-
// 反转右半部分链表
161152
ListNode* pre = nullptr;
162153
while (cur) {
163154
ListNode* t = cur->next;
@@ -167,8 +158,6 @@ public:
167158
}
168159
cur = head;
169160

170-
// 此时 cur, pre 分别指向链表左右两半的第一个节点
171-
// 合并
172161
while (pre) {
173162
ListNode* t = pre->next;
174163
pre->next = cur->next;
@@ -189,17 +178,14 @@ public:
189178
* }
190179
*/
191180
func reorderList(head *ListNode) {
192-
// 快慢指针找到链表中点
193181
fast, slow := head, head
194182
for fast.Next != nil && fast.Next.Next != nil {
195183
slow, fast = slow.Next, fast.Next.Next
196184
}
197185

198-
// cur 指向右半部分链表
199186
cur := slow.Next
200187
slow.Next = nil
201188

202-
// 反转右半部分链表
203189
var pre *ListNode
204190
for cur != nil {
205191
t := cur.Next
@@ -208,8 +194,6 @@ func reorderList(head *ListNode) {
208194
}
209195
cur = head
210196

211-
// 此时 cur, pre 分别指向链表左右两半的第一个节点
212-
// 合并
213197
for pre != nil {
214198
t := pre.Next
215199
pre.Next = cur.Next
@@ -236,20 +220,27 @@ func reorderList(head *ListNode) {
236220
Do not return anything, modify head in-place instead.
237221
*/
238222
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;
244228
}
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;
253244
}
254245
}
255246
```
@@ -305,19 +296,16 @@ impl Solution {
305296
* @return {void} Do not return anything, modify head in-place instead.
306297
*/
307298
var reorderList = function (head) {
308-
// 快慢指针找到链表中点
309299
let slow = head;
310300
let fast = head;
311301
while (fast.next && fast.next.next) {
312302
slow = slow.next;
313303
fast = fast.next.next;
314304
}
315305

316-
// cur 指向右半部分链表
317306
let cur = slow.next;
318307
slow.next = null;
319308

320-
// 反转右半部分链表
321309
let pre = null;
322310
while (cur) {
323311
const t = cur.next;
@@ -327,8 +315,6 @@ var reorderList = function (head) {
327315
}
328316
cur = head;
329317

330-
// 此时 cur, pre 分别指向链表左右两半的第一个节点
331-
// 合并
332318
while (pre) {
333319
const t = pre.next;
334320
pre.next = cur.next;
@@ -353,19 +339,16 @@ var reorderList = function (head) {
353339
*/
354340
public class Solution {
355341
public void ReorderList(ListNode head) {
356-
// 快慢指针找到链表中点
357342
ListNode slow = head;
358343
ListNode fast = head;
359344
while (fast.next != null && fast.next.next != null) {
360345
slow = slow.next;
361346
fast = fast.next.next;
362347
}
363348

364-
// cur 指向右半部分链表
365349
ListNode cur = slow.next;
366350
slow.next = null;
367351

368-
// 反转右半部分链表
369352
ListNode pre = null;
370353
while (cur != null) {
371354
ListNode t = cur.next;
@@ -375,8 +358,6 @@ public class Solution {
375358
}
376359
cur = head;
377360

378-
// 此时 cur, pre 分别指向链表左右两半的第一个节点
379-
// 合并
380361
while (pre != null) {
381362
ListNode t = pre.next;
382363
pre.next = cur.next;
@@ -390,53 +371,4 @@ public class Solution {
390371

391372
<!-- tabs:end -->
392373

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-
442374
<!-- end -->

0 commit comments

Comments
 (0)