Skip to content

Commit 0b2c53a

Browse files
committed
四刷142
1 parent b1da038 commit 0b2c53a

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed

docs/0142-linked-list-cycle-ii.adoc

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
11
[#0142-linked-list-cycle-ii]
2-
= 142. Linked List Cycle II
2+
= 142. 环形链表 II
33

4-
:stem: latexmath
4+
https://leetcode.cn/problems/linked-list-cycle-ii/[LeetCode - 142. 环形链表 II ^]
55

6-
{leetcode}/problems/linked-list-cycle-ii/[LeetCode - Linked List Cycle II^]
6+
给定一个链表的头节点 `head` ,返回链表开始入环的第一个节点。 _如果链表无环,则返回 `null`。_
77

8-
Given a linked list, return the node where the cycle begins. If there is no cycle, return `null`.
8+
如果链表中有某个节点,可以通过连续跟踪 `next` 指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数 `pos` 来表示链表尾连接到链表中的位置(*索引从 0 开始*)。如果 `pos``-1`,则在该链表中没有环。*注意:`pos` 不作为参数进行传递*,仅仅是为了标识链表的实际情况。
99

10-
To represent a cycle in the given linked list, we use an integer `pos` which represents the position (0-indexed) in the linked list where tail connects to. If `pos` is `-1`, then there is no cycle in the linked list.
10+
*不允许修改* 链表。
1111

12-
*Note:* Do not modify the linked list.
13-
14-
.Example 1:
15-
[source]
16-
----
17-
Input: head = [3,2,0,-4], pos = 1
18-
Output: tail connects to node index 1
19-
Explanation: There is a cycle in the linked list, where tail connects to the second node.
20-
----
12+
*示例 1:*
2113

2214
image::images/0142-01.png[{image_attr}]
2315

24-
.Example 2:
25-
[source]
26-
----
27-
Input: head = [1,2], pos = 0
28-
Output: tail connects to node index 0
29-
Explanation: There is a cycle in the linked list, where tail connects to the first node.
30-
----
16+
....
17+
输入:head = [3,2,0,-4], pos = 1
18+
输出:返回索引为 1 的链表节点
19+
解释:链表中有一个环,其尾部连接到第二个节点。
20+
....
21+
22+
*示例 2:*
3123

3224
image::images/0142-02.png[{image_attr}]
3325

34-
.Example 3:
35-
[source]
36-
----
37-
Input: head = [1], pos = -1
38-
Output: no cycle
39-
Explanation: There is no cycle in the linked list.
40-
----
26+
....
27+
输入:head = [1,2], pos = 0
28+
输出:返回索引为 0 的链表节点
29+
解释:链表中有一个环,其尾部连接到第一个节点。
30+
....
31+
32+
*示例 3:*
4133

4234
image::images/0142-03.png[{image_attr}]
4335

44-
*Follow-up:*
36+
....
37+
输入:head = [1], pos = -1
38+
输出:返回 null
39+
解释:链表中没有环。
40+
....
4541

46-
Can you solve it without using extra space?
42+
*提示:*
43+
44+
* 链表中节点的数目范围在范围 `[0, 10^4^]`
45+
* `-10^5^ \<= Node.val \<= 10^5^`
46+
* `pos` 的值为 `-1` 或者链表中的一个有效索引
47+
48+
**进阶:**你是否可以使用 stem:[O(1)] 空间解决此题?
4749

4850
== 思路分析
4951

@@ -116,6 +118,15 @@ include::{sourcedir}/_0142_LinkedListCycleII_2.java[tag=answer]
116118
include::{sourcedir}/_0142_LinkedListCycleII_3.java[tag=answer]
117119
----
118120
--
121+
122+
四刷::
123+
+
124+
--
125+
[{java_src_attr}]
126+
----
127+
include::{sourcedir}/_0142_LinkedListCycleII_4.java[tag=answer]
128+
----
129+
--
119130
====
120131

121132

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ endif::[]
250250
|{doc_base_url}/0143-reorder-list.adoc[题解]
251251
|✅ 双指针+链表反正+链表合并
252252

253+
|{counter:codes2503}
254+
|{leetcode_base_url}/linked-list-cycle-ii/[142. 环形链表 II^]
255+
|{doc_base_url}/0142-linked-list-cycle-ii.adoc[题解]
256+
|✅ 双指针+数学
257+
253258
|===
254259
˚
255260
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0142_LinkedListCycleII_4 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-14 20:27:39
10+
*/
11+
public ListNode detectCycle(ListNode head) {
12+
if (head == null || head.next == null) {
13+
return null;
14+
}
15+
ListNode slow = head, fast = head;
16+
while (fast != null && fast.next != null) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
if (slow == fast) {
20+
break;
21+
}
22+
}
23+
if (slow != fast) {
24+
return null;
25+
}
26+
slow = head;
27+
while (slow != fast) {
28+
slow = slow.next;
29+
fast = fast.next;
30+
}
31+
return slow;
32+
}
33+
// end::answer[]
34+
}

0 commit comments

Comments
 (0)