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) {
        //思路为先判断是否有环,然后利用一个新的表头节点a,和slow,fast第一次相遇的节点,同时到达的位置即为入口。
        if(pHead == null || pHead.next == null) return null;
        ListNode slow = pHead;
        ListNode fast = pHead;
        ListNode a = pHead;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow==fast){
                while(a!=slow){
                    a = a.next;
                    slow = slow.next;
                }
                return a;
            }
        }
        return null;
    }
}