-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListCycle.java
More file actions
49 lines (47 loc) · 1.27 KB
/
LinkedListCycle.java
File metadata and controls
49 lines (47 loc) · 1.27 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
/*
思路:先通过快慢指针判断是否有环(是否相遇)
然后通过计算可以得出:
假设:出发点(X)到环的入口(Y)的距离为a,相遇点(Z)
X-Y:a,Y-Z:b,Z-X:c
慢指针相遇走的距离为:a+b;
快指针走的距离为:a+b+c+b;
则有公式:2(a+b) = a+b+c+b
则:a=c
则可以得出:Z-Y的距离等于X-Y的距离
所以可以有两个指针同时从X,Z出发,相遇的地方就是环的入口
*/
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
//用快慢指针判断是否有环
while(true){
if(fast == null || fast.next == null){
return null;
}
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
break;
}
}
//将快慢指针变换找到环的入口点
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}