-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path55.java
More file actions
41 lines (36 loc) · 872 Bytes
/
55.java
File metadata and controls
41 lines (36 loc) · 872 Bytes
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
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead==null || pHead.next==null) return null;
//定义两个指针,p1和p2,p1每次走1步,p2每次走2步
ListNode p1=pHead;
ListNode p2=pHead;
while(p2.next!=null && p2.next.next!=null)
{
p1=p1.next;
p2=p2.next.next;
//找到相交点
if(p1==p2)
{
p2=pHead;
while(p1!=p2)
{
p1=p1.next;
p2=p2.next;
}
if(p1==p2)
return p2;
}
}
return null;
}
}