-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntryNodeOfLoop.java
More file actions
53 lines (45 loc) · 1.37 KB
/
Copy pathEntryNodeOfLoop.java
File metadata and controls
53 lines (45 loc) · 1.37 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
package nowcoder;
import java.util.HashSet;
import java.util.Set;
public class EntryNodeOfLoop {
public static class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null) return null;
Set<ListNode> CharSet = new HashSet<>();
boolean find = true;
ListNode searcher = new ListNode(pHead.val);
searcher.next = pHead.next;
CharSet.add(pHead);
searcher = searcher.next;
while (!CharSet.contains(searcher)) {
CharSet.add(searcher);
if (searcher == null) {
find = false;
break;
}
searcher = searcher.next;
}
if (find) return searcher;
return null;
}
public static void main(String[] args) {
EntryNodeOfLoop enl = new EntryNodeOfLoop();
ListNode phead = new ListNode(1);
ListNode pNode1 = new ListNode(2);
ListNode pNode2 = new ListNode(3);
ListNode pNode3 = new ListNode(4);
ListNode pNode4 = new ListNode(5);
phead.next = pNode1;
pNode1.next = pNode2;
pNode2.next = pNode3;
pNode3.next = pNode4;
pNode4.next = phead;
System.out.println(enl.EntryNodeOfLoop(phead).val);
}
}