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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...

  3. 【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 ...

  4. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  5. 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 ...

  6. 【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 ...

  7. 【Lintcode】103.Linked List Cycle II

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  8. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  9. 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 ...

随机推荐

  1. 【BZOJ1452】[JSOI2009]Count(树状数组)

    [BZOJ1452][JSOI2009]Count(树状数组) 题面 BZOJ 洛谷 题解 数据范围这么小?不是对于每个颜色开一个什么东西记一下就好了吗. 然而我不会二维树状数组? 不存在的,凭借多年 ...

  2. bzoj4458 GTY的OJ (优先队列+倍增)

    把超级钢琴放到了树上. 这次不用主席树了..本来以为会好写一点没想到细节更多(其实是树上细节多) 为了方便,对每个点把他的那个L,R区间转化成两个深度a,b,表示从[a,b)选一个最小的前缀和(到根的 ...

  3. Android Service总结05 之IntentService

    Android Service总结05 之IntentService   版本 版本说明 发布时间 发布人 V1.0 添加了IntentService的介绍和示例 2013-03-17 Skywang ...

  4. Hadoop生态圈-Azkaban实现hive脚本执行

    Hadoop生态圈-Azkaban实现hive脚本执行 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客中在HDFS分布式系统取的数据,而这个数据的是有之前我通过MapRed ...

  5. Linux通过ssh登录其他服务器,不用输入密码

    有A(192.168.10.163)和B(192.168.10.164)两台服务器,为了使A服务器通过SSH连接B服务器时,免密登录,做以下操作. 1. 登录A(192.168.10.163)服务器( ...

  6. Codeforces 923 C. Perfect Security

    http://codeforces.com/contest/923/problem/C Trie树 #include<cstdio> #include<iostream> us ...

  7. AngularJS 启程二

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...

  8. JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略

    一,非持久订阅者 和 实时消费消息 在这篇文章中区分了Domain为Pub/Sub.Destination为Topic时,消费者有两种:持久订阅者 和 非持久订阅者. 对于持久订阅者而言,只要订阅了某 ...

  9. JMS学习(三)ActiveMQ Message Persistence

    1,JMS规范支持两种类型的消息传递:persistent and non-persistent.ActiveMQ在支持这两种类型的传递方式时,还支持消息的恢复.中间状态的消息(message are ...

  10. 20155203 2016-2017-2 《Java程序设计》第7周学习总结

    20155203 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 1.Lambda表达式.(使用interface函数接口) 2.Lambda的方法参考Met ...