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. Android Tab -- 使用ViewPager、Fragment、FragmentPagerAdapter来实现

    原文地址:http://blog.csdn.net/crazy1235/article/details/42678877 效果:滑动切换:点击标签切换. 代码:https://github.com/l ...

  2. My97DatePicker日期范围限制

    1.动态时间范围限制: 可以通过系统给出的动态变量,如%y(当前年),%M(当前月)等来限制日期范围,还可以通过{}进行表达式运算,如:{%d+1}:表示明天. 格式 说明 %y  当前年 %M  当 ...

  3. C#回顾 - 3.NET的IO:字节流

    使用 Stream 类管理字节流 使用 FileStream 类管理文件数据 使用 MemoryStream 类管理内存数据 使用 BufferedSream 类提高流性能   3.1 FileStr ...

  4. Delphi中uses在interfeace和implementation中的区别

    use单元引入分为在interface中引入,如 interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...

  5. Pyqt 音视频播放器

    在寻找如何使用Pyqt做一个播放器时首先找到的是openCV2 openCV2 貌似太强大了,各种关于图像处理的事情它都能完成,如 读取摄像头.图像识别.人脸识别.  图像灰度处理 . 播放视频等,强 ...

  6. 【网络资料】Astar算法详解

    关于A*算法,很早就想写点什么,可是貌似天天在忙活着什么,可事实又没有做什么,真是浮躁啊!所以今晚还是来写一下总结吧! A*算法是很经典的只能启发式搜索算法,关于只能搜索算法和一般的搜索算法(例如DF ...

  7. Java获取当前时间年月日、时间格式化打印、字符串转日期

    package com.sysc.simple; import java.text.ParseException; import java.text.SimpleDateFormat; import ...

  8. 攻城狮在路上(贰) Spring(四)--- Spring BeanFactory简介

    BeanFactory时Spring框架最核心的接口,它提供了高级IoC的配置机制,使管理不同类型的Java对象成为了可能.我们一般称BeanFactory为IoC容器.BeanFactory是Spr ...

  9. windows下R语言在终端的运行

    在windows下可以有多种方式来运行R,R导论的这些章节给出一些详细的指导. 通常在环境变量离包含R的安装目录类似于R\R-3.1.2\bin\x64的情况下,就可以在CMD下运行R程序了 注意我这 ...

  10. supervisor(二)event

    supervisor的event机制其实,就是一个监控/通知的框架.抛开这个机制实现的过程来说的话,event其实就是一串数据,这串数据里面有head和body两部分.咱们先弄清楚event数据结构, ...