Skip to content

Commit b1da038

Browse files
committed
一刷143
1 parent 35f03ca commit b1da038

File tree

7 files changed

+118
-22
lines changed

7 files changed

+118
-22
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
10271027
|Medium
10281028
|
10291029

1030-
//|{counter:codes}
1031-
//|{leetcode_base_url}/reorder-list/[143. Reorder List^]
1032-
//|{source_base_url}/_0143_ReorderList.java[Java]
1033-
//|{doc_base_url}/0143-reorder-list.adoc[题解]
1034-
//|Medium
1035-
//|
1030+
|{counter:codes}
1031+
|{leetcode_base_url}/reorder-list/[143. Reorder List^]
1032+
|{source_base_url}/_0143_ReorderList.java[Java]
1033+
|{doc_base_url}/0143-reorder-list.adoc[题解]
1034+
|Medium
1035+
|
10361036

10371037
|{counter:codes}
10381038
|{leetcode_base_url}/binary-tree-preorder-traversal/[144. Binary Tree Preorder Traversal^]

docs/0143-reorder-list.adoc

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,75 @@
11
[#0143-reorder-list]
2-
= 143. Reorder List
2+
= 143. 重排链表
33

4-
{leetcode}/problems/reorder-list/[LeetCode - Reorder List^]
4+
https://leetcode.cn/problems/reorder-list/[LeetCode - 143. 重排链表 ^]
55

6-
Given a singly linked list _L_: _L_~0~→_L_~1~→…→_L_~_n_-1~→_L_~n~,
6+
给定一个单链表 `L` 的头节点 `head` ,单链表 `L` 表示为:
77

8+
....
9+
L0 → L1 → … → Ln - 1 → Ln
10+
....
811

9-
reorder it to: _L_~0~→_L_~_n_~→_L_~1~→_L_~_n_-1~→_L_~2~→_L_~_n_-2~→…
12+
请将其重新排列后变为:
1013

11-
You may *not* modify the values in the list's nodes, only nodes itself may be changed.
14+
....
15+
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
16+
....
1217

13-
*Example 1:*
18+
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
1419

15-
[subs="verbatim,quotes,macros"]
16-
----
17-
Given 1->2->3->4, reorder it to 1->4->2->3.
18-
----
20+
*示例 1:*
1921

20-
*Example 2:*
22+
image::images/0143-01.png[{image_attr}]
2123

22-
[subs="verbatim,quotes,macros"]
23-
----
24-
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
24+
....
25+
输入:head = [1,2,3,4]
26+
输出:[1,4,2,3]
27+
....
2528

26-
----
29+
*示例 2:*
30+
31+
image::images/0143-02.png[{image_attr}]
32+
33+
....
34+
输入:head = [1,2,3,4,5]
35+
输出:[1,5,2,4,3]
36+
....
37+
38+
*提示:*
2739

40+
* 链表的长度范围为 `[1, 5 * 10^4^]`
41+
* `+1 <= node.val <= 1000+`
2842
2943
44+
== 思路分析
45+
46+
. 寻找中点
47+
. 链表反转
48+
. 合并链表
49+
3050
[[src-0143]]
51+
[tabs]
52+
====
53+
一刷::
54+
+
55+
--
3156
[{java_src_attr}]
3257
----
3358
include::{sourcedir}/_0143_ReorderList.java[tag=answer]
3459
----
60+
--
61+
62+
// 二刷::
63+
// +
64+
// --
65+
// [{java_src_attr}]
66+
// ----
67+
// include::{sourcedir}/_0143_ReorderList_2.java[tag=answer]
68+
// ----
69+
// --
70+
====
71+
72+
73+
== 参考资料
74+
3575

docs/images/0143-01.png

26 KB
Loading

docs/images/0143-02.png

30.4 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ include::0141-linked-list-cycle.adoc[leveloffset=+1]
368368

369369
include::0142-linked-list-cycle-ii.adoc[leveloffset=+1]
370370

371-
// include::0143-reorder-list.adoc[leveloffset=+1]
371+
include::0143-reorder-list.adoc[leveloffset=+1]
372372

373373
include::0144-binary-tree-preorder-traversal.adoc[leveloffset=+1]
374374

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ endif::[]
245245
|{doc_base_url}/0415-add-strings.adoc[题解]
246246
|✅ 双指针
247247

248+
|{counter:codes2503}
249+
|{leetcode_base_url}/reorder-list/[143. Reorder List^]
250+
|{doc_base_url}/0143-reorder-list.adoc[题解]
251+
|✅ 双指针+链表反正+链表合并
252+
248253
|===
249254
˚
250255
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
import com.diguage.util.ListNodes;
5+
6+
import java.util.Arrays;
7+
8+
public class _0143_ReorderList {
9+
// tag::answer[]
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2025-04-13 22:09:48
13+
*/
14+
public void reorderList(ListNode head) {
15+
ListNode slow = head, fast = head;
16+
while (fast != null && fast.next != null) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
}
20+
ListNode reversed = reverse(slow.next, null);
21+
slow.next = null;
22+
ListNode next = head.next;
23+
boolean order = false;
24+
ListNode curr = head;
25+
while (next != null && reversed != null) {
26+
if (order) {
27+
curr.next = next;
28+
next = next.next;
29+
} else {
30+
curr.next = reversed;
31+
reversed = reversed.next;
32+
}
33+
curr = curr.next;
34+
order = !order;
35+
}
36+
curr.next = next;
37+
}
38+
39+
private ListNode reverse(ListNode head, ListNode pre) {
40+
if (head == null) {
41+
return pre;
42+
}
43+
ListNode result = reverse(head.next, head);
44+
head.next = pre;
45+
return result;
46+
}
47+
// end::answer[]
48+
public static void main(String[] args) {
49+
new _0143_ReorderList().reorderList(ListNodes.build(Arrays.asList(1, 2, 3, 4, 5)));
50+
}
51+
}

0 commit comments

Comments
 (0)