友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
142. 环形链表 II
给定一个链表的头节点 head
,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
。
如果链表中有某个节点,可以通过连续跟踪 next
指针再次到达,则链表中存在环。为了表示给定链表中的环,评测系统内部使用整数 pos
来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos
是 -1
,则在该链表中没有环。注意:pos
不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:

输入:head = [3,2,0,-4], pos = 1 输出:返回索引为 1 的链表节点 解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:

输入:head = [1,2], pos = 0 输出:返回索引为 0 的链表节点 解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:

输入:head = [1], pos = -1 输出:返回 null 解释:链表中没有环。
提示:
-
链表中节点的数目范围在范围
[0, 104]
内 -
-105 <= Node.val <= 105
-
pos
的值为-1
或者链表中的一个有效索引
进阶:你是否可以使用 \(O(1)\) 空间解决此题?
思路分析
这是 Floyd’s Tortoise and Hare (Cycle Detection) 算法。

\begin{aligned} 2 \cdot \text { distance }(\text { tortoise }) &=\text { distance }(\text {hare}) \\ 2(F+a) &=F+a+b+a \\ 2 F+2 a &=F+2 a+b \\ F &=b \end{aligned}
这里解释一下:兔子跑的快,相遇时,兔子跑过的距离是乌龟跑过的距离的两倍。兔子沿着环,跑了一圈多,有多跑了 a
才相遇,所以距离是: \(F+a+b+a\)。
这个思路还可以用于解决 287. Find the Duplicate Number。











-
一刷
-
二刷
-
三刷
-
四刷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Runtime: 1 ms, faster than 32.38% of Java online submissions for Linked List Cycle II.
* Memory Usage: 41.2 MB, less than 6.32% of Java online submissions for Linked List Cycle II.
*/
public ListNode detectCycle(ListNode head) {
if (Objects.isNull(head)) {
return null;
}
ListNode intersect = getIntersect(head);
if (Objects.isNull(intersect)) {
return null;
}
ListNode pointer1 = head;
ListNode pointer2 = intersect;
while (pointer1 != pointer2) {
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
return pointer1;
}
private ListNode getIntersect(ListNode head) {
if (Objects.isNull(head)) {
return null;
}
ListNode tortoise = head;
ListNode hare = head;
while (Objects.nonNull(hare) && Objects.nonNull(hare.next)) {
tortoise = tortoise.next;
hare = hare.next.next;
if (Objects.equals(tortoise, hare)) {
return tortoise;
}
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-07-02 21:08:43
*/
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head;
ListNode fast = head;
// 检查是否有环
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
// 没有环,直接返回
if (fast == null || fast.next == null) {
return null;
}
// 寻找环的入口节点
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-14 17:02:00
*/
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next;
fast = fast.next;
if (slow == fast) {
break;
}
}
if (fast == null || fast.next == null) {
return null;
}
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
if (slow == fast) {
break;
}
}
return slow;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2025-04-14 20:27:39
*/
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
break;
}
}
if (slow != fast) {
return null;
}
slow = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}