-
Notifications
You must be signed in to change notification settings - Fork 17
/
Que55.java
46 lines (33 loc) · 886 Bytes
/
Que55.java
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
/**
* @author: hyl
* @date: 2019/08/15
**/
public class Que55 {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null){
return null;
}
ListNode fastNode = pHead;
ListNode slowNode = pHead;
while (fastNode != null && slowNode != null){
fastNode = fastNode.next.next;
slowNode = slowNode.next;
if (slowNode == fastNode){
fastNode = pHead;
while (fastNode != slowNode){
fastNode = fastNode.next;
slowNode = slowNode.next;
}
return fastNode;
}
}
return null;
}
}