Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
leetcode:Linked List Cycle II
http://blog.csdn.net/todorovchen/article/details/24052849
ListNode *detectCycle(ListNode *head)
{
if(head==NULL)
return NULL;
ListNode *slow = head, *fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)// first meet
{
slow=head;
while(slow != fast)
{
slow = slow->next;
fast = fast->next;
}
return slow;// second meet
}
}
return NULL;
}