forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectCycle_142.java
More file actions
39 lines (38 loc) · 1.32 KB
/
Copy pathDetectCycle_142.java
File metadata and controls
39 lines (38 loc) · 1.32 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
public class DetectCycle_142 {
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* 采用双指针法:快慢指针分别一次走两步或者一步,当快慢指针相遇时候快指针走的步数为慢指针的两倍
* 假设头节点到环入口节点的长度为a(不包括入口节点),环的长度为b
* 第一次相遇时快指针走的步数为f,慢指针走的步数为s
* f = s + n * b; f = 2s; -> s = n * b; f = 2n * b;
* 走到入口节点的步数为:k = a + nb -> 只要慢指针再走a步即可
* 此时只需要把快指针置于首节点,然后快慢指针每轮走一步,直至相遇即可定位环的入口点
* @param head
* @return
*/
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
//第一次相遇
if(fast == slow) {
fast = head;
while(slow != fast) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
}