Leetcode 142 Linked List Cycle II
struct ListNode
{
int val;
ListNode* next;
ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
bool iscycle = false;
while(fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
{
slow = head;
while (fast != slow) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return NULL;
}
};