forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT55.java
More file actions
29 lines (27 loc) · 735 Bytes
/
T55.java
File metadata and controls
29 lines (27 loc) · 735 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
package web; /**
* @program LeetNiu
* @description: 链表中环的入口结点
* @author: mf
* @create: 2020/01/16 14:13
*/
import java.util.HashMap;
/**
* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
*/
public class T55 {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (null == pHead) {
return null;
}
HashMap<ListNode, Integer> map = new HashMap<>();
map.put(pHead, 1);
while (null != pHead.next) {
if (map.containsKey(pHead.next)) {
return pHead.next;
}
map.put(pHead.next, 1);
pHead = pHead.next;
}
return null;
}
}