原题链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/
目录
题目描述
思路分析
代码实现
1. 题目描述
2. 思路分析
如果有小伙伴不了解环形链表,可以先看看这篇文章:
https://blog.csdn.net/m0_62531913/article/details/132352203?spm=1001.2014.3001.5502
我们来看下图:
我们根据这个结论就可以做出这道题目了!
3. 代码实现
/**
* 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&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
struct ListNode *meet=slow;
while(head!=meet)
{
head=head->next;
meet=meet->next;
}
return meet;
}
}
return NULL;
}