【leetcode刷题笔记】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?
判断一个链表是否有环。
题解:
设置两个指针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;
}
}

如上图所示,假设从链表起点到环的起点距离为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的更多相关文章
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
随机推荐
- oracle字符串处理相关
函数 返回 描述 例子 to_char(timestamp, text) text 把 timestamp 转换成 string to_char(timestamp 'now','HH12:MI:SS ...
- POJ1122_FDNY to the Rescue!(逆向建图+最短路树)
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2368 Accepted: 72 ...
- linux sublime python
(三)配置python3编译环境 1.点击上部菜单栏Tools->Build System ->new Build System 2.点击之后,会出现一个空的配置文件,此时,往这个空配置文 ...
- java.lang.IllegalArgumentException: Request header is too large的解决方法
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&q ...
- oracle、MySQL、SQL Server的比较
MySql的优点:1 MYSQL支持5000万条记录的数据仓库(3.x版本就支持了) 2 MYSQL适应所有平台 . 3 MYSQL是开源软件,开源的东西似乎总是比较实在一点,不会有太多商业化的考虑. ...
- 《Python基础教程》第20章学习笔记
python实现:https://github.com/captainwong/instant_markup c++实现:https://github.com/captainwong/instant_ ...
- NFS网络文件系统服务(配置实战)
NFS网络文件系统服务(实战) NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.让不同的主机系统(NFS的客户端)可以透明地读写位 ...
- 请描述Java中的时间监听机制?
1.时间监听涉及到三个组件:事件源.事件对象.事件监听器 2.当事件源上发生某个动作时,它会调用事件监听器的一个方法,并将事件对象穿进去,开发人员在监听器中通过事件对象,拿到事件源,从而对事件源进行操 ...
- 使用JMeter测试Java项目
一. Apache JMeter工具 1)简介 JMeter——一个100%的纯Java桌面应用,它是Apache组织的开放源代码项目,它是功能和性能测试的工具.JMeter可以用于测试静态或者动态资 ...
- Java 获取指定日期的方法汇总
import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; impo ...