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?

借用博客http://www.cnblogs.com/hiddenfox/p/3408931.html的图

设环的距离为L = (b+c),无环的距离为a,

假设在时间t相遇,慢指针行驶距离为x,则 1 x t = x,即t=x

则快指针行驶的距离为2t = 2x,

则慢指针环上停留点为(x-a)%L,快指针停留点为 (2x-a)%L,由于在t时刻相遇,故(x-a)%L = (2x-a)%L,

根据同余定理 x%L = 0,

当快指针相遇后减慢速度为1,快指针从z继续行走a长度停下,则行走的路程为2x+a,在环上的位置为(2x+a-a)%L = 2x%L=2(x%L) = 0,即回到环的起始点,故知道环的起始点

ListNode* hasCycle(ListNode* head){
if(head == NULL || head->next == NULL) return false;
ListNode* first = head, *second = head;
while(second!=NULL && second->next!=NULL){
first = first->next;
second = second->next->next;
if(first == second) return first;
}
return NULL;
} ListNode *detectCycle(ListNode *head){
ListNode* cycleNode = hasCycle(head);
if(cycleNode != NULL){
ListNode* startNode = head;
while(startNode!=cycleNode){
cycleNode = cycleNode->next;
startNode = startNode->next;
}
}
return cycleNode;
}

Leetcode Linked List Cycle II的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  5. [LeetCode] Linked List Cycle II, Solution

    Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. LeetCode——Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. [LeetCode] Linked List Cycle II 链表环起始位置

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. 【翻译十三】java-并发之饥饿与活锁

    Starvation and Livelock Starvation and livelock are much less common a problem than deadlock, but ar ...

  2. android 入门-R文件的死与活

    1.图片的名字Btn_Play R文件死了. 1.答:修改图片的名字btn_play R文件活了.

  3. WPF MVVM模式下实现ListView下拉显示更多内容

    在手机App中,如果有一个展示信息的列表,通常会展示很少一部分,当用户滑动到列表底部时,再加载更多内容.这样有两个好处,提高程序性能,减少网络流量.这篇博客中,将介绍如何在WPF ListView中实 ...

  4. linux的<pthread.h>

    转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...

  5. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  6. IMAGE_LOAD_CONFIG_DIRECTORY64 SafeSEH检测 表

    IMAGE_LOAD_CONFIG_DIRECTORY64 typedef struct { DWORD Size; DWORD TimeDateStamp; WORD MajorVersion; W ...

  7. Pushlet浏览器长连接通讯

    原文链接:http://cuisuqiang.iteye.com/blog/1416771 Pushlet(一种comet 架构的实现)是基于Servlet 机制,数据从server端的Java 对象 ...

  8. suse 不能远程登录

    公司部分机器新装了suse企业版12,远程登录不成功,解决方法如下: 1.关闭防火墙 chkconfig --level SuSEfirewall2_init off 2.配置sshd 3.重启ssh ...

  9. RobotFramework自动化测试之环境搭建安装教程(一)

    RobotFramework是基于Python语言的工具,所以装RF之前要先安装Python: Python现在有2.7跟3.5两个版本,如果是先装了3.5的话,是装不了2.7的.只有先装2.7的 才 ...

  10. CentOS VMware 配置IP小结 静态 配置 桥接 NAT

    系统启动后可先ping下外网或局域网内其它机器. 如果配置虚拟机时选择的NAT上网方式,后面需要配置固定IP,请先参见VMware NAT方式下设置静态IP获得可用的IP范围和网关等信息. 先将ifc ...