static public ListNode FindMeetingnode(ListNode pHead)
{
if (pHead==null)
return null;
ListNode slow = pHead.next;
if (slow!=null)
return null;
ListNode quick = slow.next;
while (slow!=null&&quick!=null)
{
if (slow==quick)
return slow;
slow = slow.next;
quick = quick.next;
if (slow != quick)
quick=quick.next;
}
return null;
}
static public ListNode EntryNodeOfLoop2(ListNode pHead)
{
ListNode meetingnode = FindMeetingnode(pHead);
if (meetingnode ==null)
return null;
int nodesInloop = 1;
ListNode p1 = meetingnode;
while (p1.next!=meetingnode)
{
p1 = p1.next;
++nodesInloop;
}
p1 = pHead;
for (int i = 0; i < nodesInloop; i++) {
p1 =p1.next;
}
ListNode p2 = pHead;
while ( p1 !=p2)
{
p1 =p1.next;
p2 = p2.next;
}
return p1;
}