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. 08python之列表的常用方法

    列表list是python常用的数据类型,需要掌握以下常用方法: name_list = ['alex','tenglan','65brother'] 这个变量和之前的变量只存一个数字或字符串,这个列 ...

  2. easyui 时间定格为 时分

    $.fn.datetimebox.defaults.formatter = function (date) { console.log('dt formatting ' + date); if (!( ...

  3. Android使用百度定位API时获取的地址信息为null

    option.setAddrType("all"); //加上这个配置后才可以取到详细地址信息

  4. 《C++ Primer Plus》第10章 对象和类 学习笔记

    面向对象编程强调的是程序如何表示数据.使用 OOP 方法解决编程问题的第一步是根据它与程序之间的接口来描述数据,从而指定如何使用数据.然后,设计一个类来实现该接口.一般来说,私有数据成员存储信息,公有 ...

  5. Java Integer常量池

    public class IntegerExample { public static void main(String[] javalatte) { Integer i = 10; Integer ...

  6. JDBC处理文本和二进制文件

    JDBC支持文本(CLOB)和二进制(BLOB)文件的处理,比如要往数据库里存取文章或者图片.这都是用流的思想来解决的. 来两个Demo看看JDBC是怎么操作文本和二进制文件的. CLOB: pack ...

  7. Android UsageStats:应用根据启动次数、启动时间、应用名称排序

    Android 7.1.1 developers/samples/android/system/AppUsageStatistics/Application/src/main/java/com/exa ...

  8. Python GUI--Tkinter实践

    之前写了Testlink自动执行程序,现使用Tkinter加上GUI试试,想要实现如下图功能 可以实现通过选择要执行的url及报告url自动执行用例,或可以直接写报告结果内容 因项目原因,只列出部分代 ...

  9. maven学习(二)(转)

    一.maven父工程与子模块的拆分与聚合原理 问题描述:将ssh工程拆分为多个模块开发 1.1.拆分原理 创建一个maven project(pom),然后在创建三个子模块(maven moudule ...

  10. Ubuntu 16.04 安装和配置 Redis

    因为发现之前手动安装的 redis 与现有的教程不一样,所以总结统一一下安装的标准步骤. 安装依赖项 为了获取最新版本的 Redis,我们将从源代码进行编译和安装.下载源代码之前,需要先安装一些编译所 ...