Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cc30d3c

Browse files
committedApr 10, 2025·
二刷92
1 parent 2a6d0b3 commit cc30d3c

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed
 

‎docs/0092-reverse-linked-list-ii.adoc

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
[#0092-reverse-linked-list-ii]
2-
= 92. Reverse Linked List II
2+
= 92. 反转链表 II
33

4-
{leetcode}/problems/reverse-linked-list-ii/[LeetCode - Reverse Linked List II^]
4+
https://leetcode.cn/problems/reverse-linked-list-ii/[LeetCode - 92. 反转链表 II ^]
55

6-
Reverse a linked list from position `m` to `n`. Do it in one-pass.
6+
给你单链表的头指针 `head` 和两个整数 `left``right`,其中 `left \<= right` 。请你反转从位置 `left` 到位置 `right` 的链表节点,返回 *反转后的链表*
77

8-
*Note:* 1 ≤ m ≤ n ≤ length of list.
8+
*示例 1:*
99

10-
.Example:
11-
----
12-
Input: 1->2->3->4->5->NULL, m = 2, n = 4
13-
Output: 1->4->3->2->5->NULL
14-
----
10+
image::images/0092-01.jpg[{image_attr}]
11+
12+
....
13+
输入:head = [1,2,3,4,5], left = 2, right = 4
14+
输出:[1,4,3,2,5]
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入:head = [5], left = 1, right = 1
21+
输出:[5]
22+
....
23+
24+
*提示:*
25+
26+
* 链表中节点数目为 `n`
27+
* `+1 <= n <= 500+`
28+
* `+-500 <= Node.val <= 500+`
29+
* `+1 <= left <= right <= n+`
1530
16-
== 解题分析
31+
*进阶:* 你可以使用一趟扫描完成反转吗?
32+
33+
34+
== 思路分析
1735

1836
将链表拆分成三段(注意保存必要的访问节点):
1937

@@ -24,10 +42,26 @@ Output: 1->4->3->2->5->NULL
2442
最后再将三个拼接在一起。
2543

2644
[[src-0092]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
2750
[{java_src_attr}]
2851
----
2952
include::{sourcedir}/_0092_ReverseLinkedListII.java[tag=answer]
3053
----
54+
--
55+
56+
二刷::
57+
+
58+
--
59+
[{java_src_attr}]
60+
----
61+
include::{sourcedir}/_0092_ReverseLinkedListIi_2.java[tag=answer]
62+
----
63+
--
64+
====
3165

3266
== 思考题
3367

‎docs/images/0092-01.jpg

19.4 KB
Loading

‎logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ endif::[]
225225
|{doc_base_url}/0236-lowest-common-ancestor-of-a-binary-tree.adoc[题解]
226226
|✅ 递归遍历查找目标节点,找到则返回,找不到则返回 `null`,当左右都不是 `null` 时,则当前节点即是最近公共祖先。
227227

228+
|{counter:codes2503}
229+
|{leetcode_base_url}/reverse-linked-list-ii/[92. 反转链表 II^]
230+
|{doc_base_url}/0092-reverse-linked-list-ii.adoc[题解]
231+
|✅ 链表反转
232+
228233
|===
229234

230235
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 _0092_ReverseLinkedListIi_2 {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-04-10 21:33:16
14+
*/
15+
public ListNode reverseBetween(ListNode head, int m, int n) {
16+
if (m == n) {
17+
return head;
18+
}
19+
ListNode dummy = new ListNode(0);
20+
dummy.next = head;
21+
ListNode pre = dummy;
22+
int a = m;
23+
while (a > 1) {
24+
pre = head;
25+
head = head.next;
26+
a--;
27+
}
28+
pre.next = null;
29+
ListNode[] result = reverse(head, null, n - m + 1);
30+
head.next = result[1];
31+
pre.next = result[0];
32+
return dummy.next;
33+
}
34+
35+
private ListNode[] reverse(ListNode head, ListNode pre, int n) {
36+
if (head == null) {
37+
return new ListNode[]{pre, null};
38+
}
39+
if (n == 1) {
40+
ListNode next = head.next;
41+
head.next = pre;
42+
return new ListNode[]{head, next};
43+
}
44+
ListNode[] result = reverse(head.next, head, n - 1);
45+
head.next = pre;
46+
return result;
47+
}
48+
// end::answer[]
49+
50+
public static void main(String[] args) {
51+
new _0092_ReverseLinkedListIi_2().reverseBetween(ListNodes.build(Arrays.asList(1, 2, 3, 4, 5)), 2, 4);
52+
}
53+
}

0 commit comments

Comments
 (0)
Please sign in to comment.