Linked List Cycle leetcode II java (寻找链表环的入口)
题目:
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?
题解:
这个连同I都是很经典的题啦,刷CC150时候就折磨了半天。
其实就推几个递推公式就好。。首先看图(图引用自CC150):

从链表起始处到环入口长度为:a,从环入口到Faster和Slower相遇点长度为:x,整个环长为:c。
明确了以上信息,就可以开始做运算了。。
假设从开始到相遇,Slower走过的路程长为s,由于Faster的步速是Slower的2倍,那么Faster在这段时间走的路程长为2s。
而对于Faster来说,他走的路程还等于之前绕整个环跑的n圈的路程nc,加上最后这一次遇见Slower的路程s。
所以我们有:
2s = nc + s
对于Slower来说,他走的路程长度s还等于他从链表起始处到相遇点的距离,所以有:
s = a + x
通过以上两个式子代入化简有:
a + x = nc
a = nc - x
a = (n-1)c + c-x
a = kc + (c-x)
那么可以看出,c-x,就是从相遇点继续走回到环入口的距离。上面整个式子可以看出,如果此时有个pointer1从起始点出发并且同时还有个pointer2从相遇点出发继续往前走(都只迈一步),那么绕过k圈以后, pointer2会和pointer1在环入口相遇。这样,换入口就找到了。
Reference: http://blog.csdn.net/xiaxia__/article/details/19356861
代码如下:
1 public ListNode detectCycle(ListNode head) {
2 if(head==null||head.next==null)
3 return null;
4
5 ListNode fast = head,slow=head;
6 while (true) {
7 if (fast == null || fast.next == null) {
8 return null;
9 }
slow = slow.next;
fast = fast.next.next;
if(fast==slow)
break;
}
slow = head;//slow back to start point
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow; //when slow == fast, it is where cycle begins
}
Linked List Cycle leetcode II java (寻找链表环的入口)的更多相关文章
- [LeetCode] 141&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- Linked List Cycle I&&II——快慢指针(II还没有完全理解)
Linked List Cycle I Given a linked list, determine if it has a cycle in it. Follow up: Can you solve ...
- 141. Linked List Cycle - LeetCode
Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...
- Linked List Cycle leetcode java (链表检测环)
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- Leetcode | Linked List Cycle I && II
一.判断链表是否存在环,办法为: 设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针 ...
- Linked List Cycle——LeetCode
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- Linked List Cycle - LeetCode
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode 141. 环形链表(Linked List Cycle) 19
141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...
- 【Leetcode】287. 寻找重复数(数组模拟链表的快慢指针法)
寻找重复数 根据题意,数组中的数字都在1~n之间,所以数字的范围是小于数组的范围的,数组的元素可以和数组的索引相联系. 例如:nums[0] = 1 即可以将nums[0]作为索引 通过nums[0] ...
随机推荐
- 使用Generator(小黑鸟)反向生成Java项目(IDEA + Maven)
一.生成Maven项目 二.配置pom.xml文件 通用代码 <properties> <!-- 设置项目编码编码 --> <project.build.sourceEn ...
- 四、django rest_framework源码之频率控制剖析
1 绪言 权限判定之后的下一个环节是访问频率控制,本篇我们分析访问频率控制部分源码. 2 源码分析 访问频率控制在dispatch方法中的initial方法调用check_throttles方法开始. ...
- [ 原创 ] Java基础9--final throw throws finally的区别
final修饰的类不可被继承,final修饰的方法可以被继承但不能被重写(覆盖) final用于可以声明属性和方法,分别表示属性的不可变及方法的不可覆盖.不是方法的不可继承 throw是用来明确地抛出 ...
- IO编程
1.文件读写 >>>f = open('/Users/michael/test.txt', 'r') >>> f.read() 'Hello, world!' &g ...
- JavaSE基础之JDBC
JavaSE基础之JDBC 1.JDBC 的步骤: ①加载数据库驱动: a.MySQL:com.mysql.jdbc.Driver: b.SQLServer:com.microsoft.jdbc.sq ...
- 「HNOI2018」转盘
「HNOI2018」转盘 现场推出了大部分结论但是只写了 \(40\) 分暴力,被贺指导踩爆,现在还有点怀念 HNOI2018 贺指导对着镜子荒野行动的日子,那几天他云球迷瞎**指点篮球,被送上指导称 ...
- bzoj hash+map+set
先对原串分组hash,查询就是看某一区间内是否出现某值. 可以每个值存一个集合,保存这个值出现的位置.(也可以建可持久化值域线段树) map<int,set<int> >很省事 ...
- python开发_logging_日志处理
在很多编程语言中,都会出现日志处理操作,python也不例外... 接下来我们来看看python中的logging模块 ''' python中,logging模块主要是处理日志的. 所谓日志,可理解为 ...
- BZOJ 1191: [HNOI2006]超级英雄Hero 匈牙利算法
1191: [HNOI2006]超级英雄Hero Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx Solved: 2xx 题目连接 http:/ ...
- [JAVA] JAVA 类路径
Java 类路径 类路径是所有包含类文件的路径的集合. 类路径中的目录和归档文件是搜寻类的起始点. 虚拟机搜寻类 搜寻jre/lib和jre/lib/ext目录中归档文件中所存放的系统类文件 搜寻再从 ...