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?


判断一个链表是否有环。

题解:

设置两个指针p1和p2;

p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环;否则两个指针必然相遇,则有环;

接下来找环的起点,将p1挪动到链表起始,p2保持在两指针相遇的地方不动,然后p1和p2分别每次走一步,则下一次相遇的地方为环的起点。

先贴代码如下:

 class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
} public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode p1 = head;
ListNode p2 = head; while(p2 != null){
p1 = p1.next;
p2 = p2.next;
if(p2 != null)
p2 = p2.next;
else
return null;
if(p1 == p2)
break;
} if(p2 == null)
return null;
p1 = head;
while(p1 != p2){
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}

证明如下(证明参考:http://stackoverflow.com/questions/2936213/explain-how-finding-cycle-start-node-in-cycle-linked-list-work

如上图所示,假设从链表起点到环的起点距离为m,从环的起点到p1和p2第一次相遇的地方距离为k,环的长度为n。设第一次相遇的时候p1走过了S步,则p2走过了2S步,所以

S = m + pn + k      (1)

2S = m + qn + k    (2)

其中p1绕圆走过了完整的p圈,p2绕圆完整的走过了q圈。

(1)式代入(2)式中得:2(m+pn+k) = m+qn + k  ->  m+k = (q - 2p)n   (3)

对于任意一个链表m和n是固定的,所以我们只要证明存在整数p,q,k使得(3)式成立即可。

去p = 0, k = mn-m, q = m, 则(3)式成立,所以p1,p2一定会相遇在距离环的起点(mn-m)的地方。

接下来证明如果使得p1回到环的起点,p2保持不动,两个指针以相同的速度前行,则下一次相遇的地方一定是环的起点。

从(3)式可以看出,m+k是环的长度的整数倍,所以p2从相遇的地方走m步,一定回到环的起点,而p1从链表起点走m步也走到环的起点,所以p1和p2以相同的速度前进,下一次相遇的地方一定的环的起点。

证毕。

【leetcode刷题笔记】Linked List Cycle II的更多相关文章

  1. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. 【leetcode刷题笔记】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 【leetcode刷题笔记】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  10. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. 一个队列类的实现(比delphi自带的速度快70倍)(线程安全版本)

    unit sfContnrs; interface {$DEFINE MULTI_THREAD_QUEUE} //线程安全版本,如果不需要线程安全,请注释掉此行代码 {$IFDEF MULTI_THR ...

  2. GDBus

    1. https://en.wikipedia.org/wiki/D-Bus In computing, D-Bus (for "Desktop Bus"[4]), a softw ...

  3. java.String中的方法

    (String) str.trim() 该方法返回一个复制该字符串的开头和结尾的白色空格去掉,或字符串,如果它没有头或尾空白. (Boolean) str.contains(str1) 判断 str ...

  4. JAVA实现EXCEL公式专题(七)——统计函数

    统计函数主要实现的是较为复杂的统计函数如countif.sumif.frequency,也是,直接上代码 /** * 项目名称: * 文件说明: * 主要特点:文件说明:EXCEL函数类型:统计函数 ...

  5. 【NOI2015】【寿司晚宴】【状压DP】

    Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴. 小 G 和小 W 作为參加 NOI 的选手,也被邀请參加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不 ...

  6. Angular 资料大集合

    https://angular.cn/   Angular 的中文网 http://www.apjs.net/#dir1    Angular 的中文网 http://www.ngnice.com/  ...

  7. eclipse导入svn工程,在文件夹里面不展示svn工程图标

    原因:没有安装subclipse插件 解决方法:在marketplace中搜索插件名字:subclipse,点击安装,或者在instal new software中输入插件名字,安装完成之后,在文件夹 ...

  8. UVA-11625-Nice Prefixes (DP+矩阵快速幂)

    题目(vjudge) 题面 题意: 你有K个字母,你需要用K个字母组成L长度的字符串,定义对于该字符串的任意前缀P 必须满足    ,输出方案数%1000000007的值. 思路: 首先可以想到一种简 ...

  9. 快速清除MS SQL SERVER 日志

    USE [master] GO ALTER DATABASE yourdatabase SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE yourd ...

  10. unity一些知识

    有一个问题就是在Inspector面板修改 WheelNumber的数值后,运行项目,当项目停止的时候,WheelNumber 的数据又回到以前的数据,(数据未保存成功,数据丢失) 解决办法需要在 修 ...