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 ...
随机推荐
- mysql中的unix_timestamp函数
偶然看到MySQL的一个函数 unix_timestamp(),不明就里,于是就试验了一番. unix_timestamp()函数的作用是返回一个确切的时间点的UNIX时间戳,这个Unix时间戳是一个 ...
- BestCoder Round #89
过了这么久才来写-- BC的后两道题好难--(第二道题也不怎么简单--) 1001 Fxx and string 正着倒着枚举一次就ok #include<iostream> #inclu ...
- XSL相关
<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="tex ...
- 【MySQL】使用 Optimizer Trace 观察SQL执行过程
Optimizer Trace 是MySQL 5.6.3里新加的一个特性,可以把MySQL Optimizer的决策和执行过程输出成文本.输出使用JSON格式,便于程序分析和人类阅读. 使用方法 1) ...
- mac下搭建cordova开发环境
Apache Cordova 原名叫PhoneGap.是一个用基于HTML,CSS和JavaScript的,创建移动跨平台移动应用程序的快速开发平台.PhoneGap最初由Nitobi开发,2011年 ...
- 大数据时代之hadoop(二):hadoop脚本解析
“兵马未动,粮草先行”,要想深入的了解hadoop,我觉得启动或停止hadoop的脚本是必须要先了解的.说到底,hadoop就是一个分布式存储和计算框架,但是这个分布式环境是如何启动,管理的呢,我就带 ...
- pigcms 微外卖下单加数量bug
bug:加数量的时候结算金钱出现NAN 先给一个简单粗暴的解决办法. 找到/tpl/static/dishout/js/main.js 把 65行 disPrice = parseFloat(sig ...
- BitSort
这个题为<编程珠玑>中提到的算法,解题思路和桶排序/基数排序一样,适用于大量没有重复的数据. 结题思路: 1.遍历整个数据文件,每提取一个数据,在BitMap中对应的位置赋1 2.遍历Bi ...
- mysql解决中文乱码问题
安装文件 my.ini default-character-set=gbk 安装文件 db.opt default-character-set=gbkdefault-collation=gbk_chi ...
- IOS 中会发生crash的操作
对字典和数组进行下列操作时会产生crash: 对于字典来说: 查询时,key=nil 或者 key=null 时都能正常运行 插入时,,key=nil 或者 key=null 都会crash 对于数组 ...