友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
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.
Note: Do not modify the linked list.
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
Follow-up:
Can you solve it without using extra space?
思路分析
这是 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;
}