题目描述:
![](https://i-blog.csdnimg.cn/blog_migrate/6f49486f33e773de3912d0efa1440884.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c868f411413591e567e92e47c6065019.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ec3b73d60c5fd90a6dcef9afa23c4521.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7a17578301f8802e999e48487c59cc15.png)
代码实现:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* fast=head,*slow=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
slow=head;
while(slow!=fast)
{
slow=slow->next;
fast=fast->next;
}
return fast;
}
}
return NULL;
}