-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectCycle.java
More file actions
40 lines (36 loc) · 1.04 KB
/
Copy pathDetectCycle.java
File metadata and controls
40 lines (36 loc) · 1.04 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
package linkedList;
/**
* @Author: wei1
* @Date: Create in 2018/12/4 12:17
* @Description: 快指针和慢指针第一个相遇时为有环,在重头来个节点与快节点相遇就是这个环的开始节点
* Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
*
* Follow up:
* Can you solve it without using extra space?
*/
public class DetectCycle {
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode begin = head;
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
break;
}
}
if (fast != slow) {
return null;
} else {
while (begin != slow) {
begin = begin.next;
slow = slow.next;
}
}
return begin;
}
}