码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode[Linked List]: Linked List Cycle II

时间:2014-11-26 18:57:10      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:leetcode   linked list   链表      双指针   

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;
}


LeetCode[Linked List]: Linked List Cycle II

标签:leetcode   linked list   链表      双指针   

原文地址:http://blog.csdn.net/chfe007/article/details/41519183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!