/**
* 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) {
if(head == NULL)
return NULL;
ListNode* slow = head;
ListNode* fast = head;
fast = fast->next;
while(slow != fast)
{
if(fast == NULL || fast->next == NULL || fast->next->next == NULL)
return NULL;
slow = slow->next;
fast = fast->next->next;
}
slow = slow->next;
ListNode* p = head;
while(p != slow)
{
p = p->next;
slow = slow->next;
}
return slow;
}
};