![](https://i-blog.csdnimg.cn/blog_migrate/4fc84fc4363bb65a3b130c6605dc85ec.png)
用快慢指针
![](https://i-blog.csdnimg.cn/blog_migrate/19aef9f8a26777aba473e22d8ceb3ac2.png)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* detectCycle(struct ListNode* head) {
struct ListNode *slow = head, *fast = head;
while (fast != NULL) {
slow = slow->next;
if (fast->next == NULL) {
return NULL;
}
fast = fast->next->next;
if (fast == slow) {
struct ListNode* ptr = head;
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return NULL;
}