public ListNode detectCycle(ListNode head)
{
ListNode slow, fast;
slow = fast = head;
while(slow != null && slow.next != null && fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
{
ListNode p = head;
while(p != slow)
{
p = p.next;
slow = slow.next;
}
return p;
}
}
return null;
}
{
ListNode slow, fast;
slow = fast = head;
while(slow != null && slow.next != null && fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
{
ListNode p = head;
while(p != slow)
{
p = p.next;
slow = slow.next;
}
return p;
}
}
return null;
}