LeetCode OJ 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
这个题还是蛮考验数学推理的,不过在前一个题的基础上还是能推出结果的。这是英文一段解释,非常有帮助。
First Step: Assume the first pointer runs from head at a speed of 1-by-1 step, as S, and the second pointer runs at a speed of 2-by-2 step, as 2S, then two pointers will meet at MEET-POINT, using the same time. Define outer loop is A, the distance from CIRCLE-START-POINT to MEET-POINT is B, and the distance from MEET-POINT to CIRCLE-START-POINT is C (Apparently, C=loop-B), then (n*loop+a+b)/2S = (a+b)/S, n=1,2,3,4,5,....
Converting that equation can get A/S=nloop/S-B/S. Since C=loop-B, get A/S = ((n-1)loop+C)/S.
That means, as second step, assuming a pointer running from head and another pointer running from MEET-POINT both at a speed S will meet at CIRCLE-START-POINT;

代码如下:
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null || head.next==null) return null;
ListNode pointer1 = head;
ListNode pointer2 = head;
while(pointer1!=null && pointer2!=null){
pointer1 = pointer1.next;
if(pointer2.next==null) return null;
pointer2 = pointer2.next.next;
if(pointer1==pointer2) break;
}
if(pointer1==null || pointer2==null) return null;
pointer1 = head;
while(pointer1 != pointer2){
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
return pointer1;
}
}
LeetCode OJ 142. Linked List Cycle II的更多相关文章
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- 【LeetCode】142. Linked List Cycle II
Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
随机推荐
- python操作----Memcached
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...
- jQuery EasyUI的使用入门
jQuery EasyUI不是什么太高级的东西,就是用jQuery写了很多方法.定义了很多属性,通过调用这些方法.属性,可以达到一些特定的效果,然后我们再根据具体需求微调就好了.至少需要导入两个样式表 ...
- 为Textview里面的ImageSpan添加点击响应事件
对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...
- Android:Asmack能登录但是获取不到联系人的问题
先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...
- openstack私有云布署实践【7.2 keystone + memcache (办公网环境)】
首先登录controller1创建keystone数据库,并赋于远程和本地访问的权限. mysql -u root -p CREATE DATABASE keystone; GRANT ALL P ...
- 使用PIE对IE进行CSS3兼容介绍和经验总结
国外团队开发的兼容插件,去年做项目时才发现,非常强大 主角:PIE.js , PIE.htc 两种方法可以实现 官方网站:http://css3pie.com/ 演示地址:http://css3pie ...
- linux jdk环境变量
export JAVA_HOME=/usr/share/jdk8 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/ ...
- vs2008试用版的评估期已经结束解决办法
1.控制面板----程序和功能----找到VS2008,打开"卸载/更改". 2.下载补丁:files.cnblogs.com/elaky/PatchVS2008.zip 打补丁之 ...
- eclipse中git插件配置 编辑
一.Eclipse上安装GIT插件EGit EGit插件地址:http://download.eclipse.org/egit/updates OK,随后连续下一步默认安装就可以,安装后进行重启Ecl ...
- 详解Bootstrap 定义按钮的样式(CSS)
以下样式可用于<a>, <button>, 或 <input> 元素上: 更多关于Bootstrap 定义CSS样式的可查看:http://v2.bootcss.c ...