题解 | #链表中环的入口结点#
链表中环的入口结点
http://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4
用一个集合存储地址,重复了代表链表成环了。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
Set<ListNode> we = new HashSet<>();
for(;pHead != null;){
if(!we.add(pHead)){
return pHead;
}
pHead = pHead.next;
}
return null;
}
}