/*
public class ListNode
{
public int val;
public ListNode next;
public ListNode (int x)
{
val = x;
}
}*/
class Solution
{
public ListNode EntryNodeOfLoop(ListNode pHead)
{
// write code here
if(pHead==null)
return null;
ListNode slow=pHead;
ListNode faster=pHead;
while(faster!=null && faster.next!=null )
{
faster=faster.next.next;
slow=slow.next;
if(faster==slow)
break;
}
if(faster==null || faster.next==null)
return null;
while(pHead!=faster)
{
pHead=pHead.next;
faster=faster.next;
}
return faster;
}
}
如果不借助hash,关键是环的计算