题目描述
思路整理
代码
/**
* 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 *flow, *fast;
flow = head;
fast = head;
while(1)
{
if(fast == NULL)
return fast;
if(fast->next == NULL)
return NULL;
fast = fast->next->next;
flow = flow->next;
if(flow == fast)
break;
}
fast = head;
while(flow != fast)
{
flow = flow->next;
fast = fast->next;
}
return flow;
}
};