您的位置:首页 > 其它

LeetCode[Linked List]: Linked List Cycle II

2014-11-26 17:47 351 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up:

Can you solve it without using extra space?

这个题目跟Linked List Cycle一样,我也没有能够自己独立找到解决方法,而是借助Discuss学到了一个非常巧妙的解决方法:

首先借助LeetCode[Linked List]: Linked List Cycle同样的方法判断是否存在环,如果存在环:

假设当fast指针和slow指针相遇时已经走了k步,又设环的长度为r,则:2k-k=nr => k=nr。也就是说已经走的步数k一定是环的整数倍,假设链表起始点与环起始点的距离为m,那么从相聚点再走m步必然到达环起始点。所以将slow重置为头结点,fast不变,仍然是相聚点,这次两个指针每次都移动一步,那么两者再次相遇时必然就是环的起始点。

以上是我根据Discuss上的观点,自己得到的解释。原文是这样解释的:

my solution is like this: using two pointers, one of them one step at a time. another pointer each take two steps. Suppose the first meet at step k,the length of the Cycle is r. so..2k-k=nr,k=nr Now, the distance between the start node of list and the start
node of cycle is s. the distance between the start of list and the first meeting node is k(the pointer which wake one step at a time waked k steps).the distance between the start node of cycle and the first meeting node is m, so...s=k-m, s=nr-m=(n-1)r+(r-m),here
we takes n = 1..so, using one pointer start from the start node of list, another pointer start from the first meeting node, all of them wake one step at a time, the first time they meeting each other is the start of the cycle.

代码实现非常简单:

ListNode *detectCycle(ListNode *head) {
    if (!head) return false;

    ListNode *slow = head, *fast = head;
    while (slow->next && fast->next && fast->next->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            for (slow = head; slow != fast; slow = slow->next, fast = fast->next) ;
            return slow;
        }
    }

    return NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: