/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *pslow = head;
ListNode *pquick = head;
while(1)
{
if(pslow)
{
pslow = pslow->next;
}
else
{
return NULL;
}
if(pquick->next && pquick->next->next)
{
pquick = pquick->next->next;
}
else
{
return NULL;
}
if(pslow == pquick)
{
pslow = head;
while(pslow!=pquick)
{
pslow = pslow->next;
pquick = pquick->next;
}
return pslow;
}
}
}
};
T is the length of
first met in C; for t step;
AB+BC = t;
AB+BC+N*T = 2t;
so
AB+BC = N*T
if N = 1;
AB+BC = T;
BC is the part of circle ;
so
CB_large = AB;
so when pslow return to A and
and pquick stay at B ;
then they begin to run at the same pace; they will meet at the B again;
this is the start of the circle