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) {
        ListNode res = null;
        HashSet<ListNode> set = new HashSet<>();
        ListNode ptr = pHead;
        while (ptr != null) {
            if (set.contains(ptr)) {
                res = ptr;
                break;
            } else {
                set.add(ptr);
                ptr = ptr.next;
            }
        }
        return res;
    }
}