我来更新 leetcode 题目了,接着上一次,这一次是上一道题目的提升(有点数学题的感觉)
142.环形链表2
代码
struct ListNode *detectCycle(struct ListNode *head)
{
struct ListNode *slow = head;
struct ListNode *fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
struct ListNode *meet = slow;
slow = head;
while(1)
{
if(meet == slow)
{
return meet;
}
else
{
meet = meet->next;
slow = slow->next;
}
}
}
}
return NULL;
}