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. ios 生成一个动态的随机的头像/随机数的操作

    在写项目的时候,可能会遇到这种情况,用到集中随机的颜色,或者头像等, 首先:把所需要的图片放进一个数组当中 imgsAry = @[@"t1.png",@"t2.png& ...

  2. socket网络编程【python】

    转自:http://www.jb51.net/article/19751.htm socket 是网络连接端点. 一.网络知识的一些介绍 socket 是网络连接端点.例如当你的Web浏览器请求www ...

  3. NPOI抓取WPS表格保存的EXCEL文件

    其实是可以抓取的,唯一不同就是Sheet的位置前进了一位.     var sheet1 = (HSSFSheet)hssfworkbook.GetSheetAt(1);     来自为知笔记(Wiz ...

  4. C# string.Format("{0:C3}", 2)

  5. lua中类的实现原理和实践

    一.基础概念  Lua 本身是函数式的语言,但借助 metatable (元表)这个强大的工具,Lua 实现操作符重载易如反掌.. 下文将详细的解释在Lua中实现类的原理,涉及到的细节点将拆分出来讲, ...

  6. Objective-c为什么要有属性

    属性:为什么要有属性 . 首先是因为实例变量的安全性和继承能力,如果我们允许被继承,子类要动这些实例变量,我们需要能够参与进来,如果子类设置了某个值,我们需要检查范围,保证不会破坏父类,保证不会破坏父 ...

  7. 【BZOJ3678】wangxz与OJ Splay

    [BZOJ3678]wangxz与OJ Description 某天,wangxz神犇来到了一个信息学在线评测系统(Online Judge).由于他是一位哲♂学的神犇,所以他不打算做题.他发现这些题 ...

  8. Android屏幕适配和文字屏幕适配

    http://blog.sina.com.cn/s/blog_9996c67e0101euwd.html 最近在一个项目中要实现屏幕适配平板和手机等不同的型号,而蛋疼的美工给了一套图,而且这些图纸有在 ...

  9. oracle的connect by level的示例

    SELECT regexp_substr('CITY_AA,CITY_BB,CITY_CC,CITY_DD', '[^,]+', 1, LEVEL) t FROM dualCONNECT BY reg ...

  10. spring的AOP个人理解和使用

    1什么是AOP:AOP是面向切面编程,也就是说面向某个功能模块编程,典型的应用就是Spring的声明式事务, Spring的AOP事务解析: 在以前的事务管理是要融合在逻辑代码中的,在逻辑代码中决定事 ...