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?

头结点到cycle begins的点 距离是A, cycle begins的点 到快慢结点相遇的 点的距离是B

A+B+N = 2*(A+B)

A+B = N

所以 快慢指针相遇后,从头结点开始再跑一个慢指针,直到2个慢的相遇,相遇的点就是cycle begin

 public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode faster = head;
ListNode slower = head; while(faster !=null && faster.next != null){
faster = faster.next.next;
slower = slower.next; if(faster == slower){
ListNode slower2 = head;
while(slower != slower2){
slower = slower.next;
slower2 = slower2.next;
}
return slower;
}
}
return null; }
}

142. Linked List Cycle II(找出链表相交的节点)的更多相关文章

  1. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

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

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

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

  5. Java for LeetCode 142 Linked List Cycle II

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

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

  7. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  8. (链表 双指针) leetcode 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  9. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

随机推荐

  1. GIS-008-ArcGIS JS API 全图

    //待服务加载完成后,设置视野范围到全图范围 layer.on('load', function () { var extent = map.getLayer(map.layerIds[0]).ful ...

  2. CH7-WEB开发(集成在一起)

  3. Python 数据类型:列表

    一.列表介绍 1. 列表可以存储一系列的值,使用中括号来定义,每个元素之间用逗号隔开,形如 ['a', 'b', 'c', 'd']2. 列表与元组的区别是:列表中的元素是可变的,元组中的元素是不可变 ...

  4. 关于微信的jsdk的若干亲身实践之小结

    前言: 业务来源:自主研发的手机app软件有分享文章到微信或者QQ以及微博的功能,而在微信中再次点击分享按钮的时候,情况就出现的不可把控了: 文章显示的缩略图不能正常显示:文章的简介不能显示……而我们 ...

  5. 本地schemeApp扩展

    作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di 本地schemeApp扩展 iHasApp这个用过的话,大概知道我说的是什么了. scheme ...

  6. LeetCode——pow(x, n)

    超时了,只能先这么干了. return Math.pow(x, n);

  7. java EE ME SE有什么关系

    1. Java SE(Java Platform,Standard Edition).Java SE 以前称为 J2SE.它允许开发和部署在桌面.服务器.嵌入式环境和实时环境中使用的 Java 应用程 ...

  8. mongodb学习链接

    mongodb安装部署:http://www.cnblogs.com/yoolonet/archive/2011/08/27/2155701.html 基础:  http://blog.csdn.ne ...

  9. scss的安装使用

    Ruby的安装 如果是Window系统,请打开:http://rubyinstaller.org/downloads/ ,下载当前稳定版本的exe文件.界面如下所示: Step(2): 接下来,在系统 ...

  10. Android中Log机制详解

    Android中Log的输出有如下几种: Log.v(String tag, String msg);        //VERBOSELog.d(String tag, String msg);   ...