【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 天的价格. ...
随机推荐
- VBA 按列查找小工具类似lookUp函数
如上图,查找A列的数据在D,F列是否存在,如果存在背景色变绿,如果不存在则A列的背景色变红. 直接贴上代码: Private Sub CommandButton1_Click() Call lookU ...
- HTML5 的四个亮点
1.XDM cross-document-messaging 跨文档消息传递. 2.原生拖放功能. 3.新媒体元素 audio.video. 4.历史状态管理.
- 小书匠markdown编辑器V1.0.12发布
a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; outline-offset: -2p ...
- Verilog 浮点数运算模块
算法中常常会到浮点数运算,而浮点数的处理常常是Verilog初学中常常遇到的问题.以下将就一个简单的例子说明Verilog中浮点数运算处理. 在JPEG图像压缩时遇到色彩空间变换的问题,将YCbCr转 ...
- rm 命令简要
rm 单独使用只能删除文件不能删除文件夹 rm -r 可以删除文件夹和文件 1.rm test.txt 删除文件 2.rm -r test.txt 每次删除的时候都询问是 ...
- bootstrap input 加了 disabled 后台竟然接受不到值
下午做了一个input 值需要不可改变.在input属性中加入了 disabled .在后台接受 var_dump($_POST); 竟然看不到值. <input type="t ...
- ubuntu 及 postgredql 安装配置小坑摘录
ubuntu 16.04.1 安装 Ubuntu Server 16.04.1安装配置图解教程,按教程修改局域网static IP 开启sftp必须 解决SSH服务拒绝密码,之后才能欢乐地使用file ...
- 【Atheros】Ath9k速率调整算法源码走读
上一篇文章介绍了驱动中minstrel_ht速率调整算法,atheros中提供了可选的的两种速率调整算法,分别是ath9k和minstrel,这两个算法分别位于: drivers\net\wirele ...
- ios 手势返回<1>2
iOS-给push出来的控制器添加全局滑动(返回)手势 在iOS中,当我们push出一个新的控制器的时候,我们可以向右拖拽屏幕的左边缘来返回(pop)到上一级控制器,但是这个功能有两个缺陷: 当自 ...
- MySql 查询列中包含数据库的关键字
MySQL查询列表中包含数据的关键字的处理办法是用``把关键字包起来(tab键上面的字符)