【LeetCode】142. Linked List Cycle II
Difficulty:medium
More:【目录】LeetCode Java实现
Description
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?
Intuition
1) find out whether there is a cycle, refer to141. Linked List Cycle
2) if there is a cycle, we can get the meetingNode, meanwhile we use a pointer named entry to point to the head Node.
3) let the meetingNode and entry move one step at a time, where they meet is where the cycle begins.
why will they meet at the beginning of the cycle? here is the proof:
1. define L1 as the distance between the head node and entry node;
2.defined L2 as the distance between the entry node and meeting node;
3.defined C as the length of the cycle

We can know that:
the distance of slow node=L1+L2; while the distance of fast node=L1+L2+n*C
because of the speed, we have :
2*(L1+L2)=L1+L2+n*C => L1=n*C-L2 => L1=(n-1)C+(C-L2)
it means that: the distance between the head node and entry node equals the distance between the meeting node and entry node;
Solution
public ListNode detectCycle(ListNode head) {
if(head==null)
return null;
ListNode fast=head;
ListNode slow=head;
ListNode entry=head;
while(fast.next!=null && fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
while(entry!=slow){
entry=entry.next;
slow=slow.next;
}
return entry;
}
}
return null;
}
Complexity
Time complexity : O(n)
Space complexity : O(1)
What I've learned
1. How to prove the distance relation.
More:【目录】LeetCode Java实现
【LeetCode】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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 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 ...
- 【LeetCode】141. Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- 【Lintcode】103.Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 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 ...
随机推荐
- 【BZOJ1047】[HAOI2007]理想的正方形(单调队列,动态规划)
[BZOJ1047][HAOI2007]理想的正方形(单调队列,动态规划) 题面 BZOJ 洛谷 题解 直接一个单调队列维护一下没给点和它前面的\(n\)个位置的最大值,再用一次单调队列维护连续\(n ...
- 【POJ1958】汉诺塔+
题目大意:给定一个四个柱子的汉诺塔,N 个盘子,求最少多少步移动到另一个柱子上. 题解:\(f[n]=min(2*f[i]+d[n-i])\),其中 \(d[i]\) 为汉诺三塔最小移动次数.M 塔同 ...
- 调试技巧 ------ printf 的使用技巧
编译器宏:__FUNCTION__,__FILE__,__LINE__ #define __debug #ifdef __debug //#define debug(format,...) print ...
- insserv: Script <name> is broken: incomplete LSB comment.
insserv: Script <name> is broken: incomplete LSB comment. insserv: missing `Required-Start:' e ...
- 在“安装”阶段发生异常。 System.Security.SecurityException: 未找到源,但未能
写Windows服务的时候,运行了一下,就是没反应,命令框一闪而过,查了一下异常,大致是题目的那样.原因是因为权限不足.但是在网上搜的方法都不顶用. 解决方法如下: (1)以管理员身份运行CMD: ( ...
- Spark记录-Scala数组
Scala提供了一种数据结构叫作数组,数组是一种存储了相同类型元素的固定大小顺序集合.数组用于存储数据集合,但将数组视为相同类型变量的集合通常更为有用. 可以声明一个数组变量,例如:numbers,使 ...
- bzoj千题计划285:bzoj2555: SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 后缀自动机,用LCT维护parent树 一个串的出现次数 = parent 树 上 其所在状态 ...
- bzoj千题计划209:bzoj1185: [HNOI2007]最小矩形覆盖
http://www.lydsy.com/JudgeOnline/problem.php?id=1185 题解去看它 http://www.cnblogs.com/TheRoadToTheGold/p ...
- [iOS]深拷贝/浅拷贝区别
来点鸡汤: // 所谓拷贝 就是在原有的对象的基础上产生一个新的副本对象.有两点原则: // 1. 改变原对象的属性和行为不会影响副本对象 // 2. 改变副本对象的属性和行为不会影响原对象 ...
- 基础知识点 关于 prototype __proto__
基础知识点 关于 prototype __proto__ 供js新手参考 JavaScript 的一些基础知识点: 在 JavaScript 中,所有对象 o 都拥有一个隐藏的原型对象(在 Fire ...