/*
 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;
    }
}