题解 | #链表中环的入口结点#
查找除复旦大学的用户信息
http://www.nowcoder.com/practice/c12a056497404d1ea782308a7b821f9c
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
//时间复杂度:O(n),空间复杂度:O(n),最坏的情况下所有的节点都要进入map
import java.util.*;
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
if(pHead.next==null){
return null;
}
if(pHead.next==pHead){
return pHead;
}
HashMap<ListNode,Integer> map = new HashMap<>();
while(pHead.next!=null){
if(map.containsKey(pHead)){
return pHead;
}
map.put(pHead,pHead.val);
pHead = pHead.next;
}
return null;
}
}