import java.util.*;
/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        Set<ListNode> listNodes=new HashSet<>();
        listNodes.add(pHead);
        while (pHead.next!=null){
            int preSize = listNodes.size();
            ListNode next = pHead.next;
            listNodes.add(next);
            int size = listNodes.size();
            if(size>preSize){
                pHead=next;
            }else {
                return next;
            }
        }
        return null;
    }
}