- 快慢指针,好像和之前的某道题相似
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
// 快慢指针
struct ListNode *fast=head;
struct ListNode *low=head;
struct ListNode *newlow=head;
while(fast && low && fast->next && fast->next->next && low->next )
{
fast=fast->next->next;
low=low->next;
if(fast==low)
{
while(newlow && low)
{
if(newlow==low)
{
return newlow;
}
newlow=newlow->next;
low=low->next;
}
}
}
return NULL;
}
我看还有哈希表的解法,明天看看
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/chun-cjie-jue-ha-xi-zhi-linked-list-cycle-ii-by-we/
ε=(´ο`*)))唉,这几天好丧啊,快点调整过来吧,天涯何处无芳草