/**
* 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 *dummy = new ListNode;
dummy->val = 0;
dummy->next = head;
ListNode *fast , *slow;
fast = dummy;
slow = dummy;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
ListNode *indnx1 = fast;
ListNode *indnx2 = dummy;
while(indnx1 != indnx2)
{
indnx1 = indnx1->next;
indnx2 = indnx2->next;
}
return indnx2;
}
}
return NULL;
}
};