LeetCode[Linked List]: Linked List Cycle II

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值