-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindEntry.java
More file actions
55 lines (49 loc) · 1.14 KB
/
findEntry.java
File metadata and controls
55 lines (49 loc) · 1.14 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
50
51
52
53
54
55
//查找环的入口点
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead)
{
if(pHead==null||pHead.next==null)
return null;
ListNode fast = new ListNode(0);
ListNode slow = new ListNode(0);
//
slow=pHead.next;
fast=pHead.next.next;
while(fast!=slow)
{
slow=slow.next;
fast=fast.next.next;
}
fast=pHead;
while(fast!=null)
{
if(fast==slow)
return fast;
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
/*
c++实现
class Solution {
public:
ListNode* EntryNodeOfLoop(ListNode* pHead)
{
if(pHead==NULL || pHead->next==NULL)
return NULL;
set<ListNode*> listSet;
while(pHead!=NULL){
//说明没有找到pHead
if(listSet.find(pHead)==listSet.end()){
listSet.insert(pHead);
pHead=pHead->next;
}
else
return pHead;
}
return NULL;
}
};
*/