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?

141题的延伸,求出循环点。

可以用数学方法证明出slow与find相遇的位置一定是所求的点。

/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
if( head == null || head.next == null )
return null;
ListNode fast = head;
ListNode slow = head; while( fast != null && fast.next != null ){
slow = slow.next;
fast = fast.next.next;
if( fast == slow ){
ListNode find = head;
while( find != slow ){
find = find.next;
slow = slow.next;
}
return find;
}
} return null; }
}

leetcode 142. Linked List Cycle II ----- java的更多相关文章

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

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

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

  4. (链表 双指针) 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 ...

  5. LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java

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

  6. leetcode 142. Linked List Cycle II

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

  7. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  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. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

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

随机推荐

  1. C++-类的const成员变量

    当类中用到一些固定值时,希望将其定义为const成员变量,防止被修改. 但因为const成员变量因为初始化之后就不能修改,因此只能在构造函数的初始化列表中初始化 如果是数组,则没有办法在初始化列表中初 ...

  2. JS编码,解码. asp.net(C#)对应解码,编码

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...

  3. flask剖析

    1.为何只要通过import request,就能拿到对应的request呢?怎么解决区分请求,区分线程的问题? 简而言之,就是通过拿栈顶对象就表示是当前活动的对象 但对于多线程,由于栈的数据结构是 ...

  4. 恢复drop数据

    select * from recyclebin r where r.original_name = 'MSM_EXAINVITEBIDSCHEMEHEAD' ; flashback table MS ...

  5. 经典线程同步 关键段CS

    上一篇<秒杀多线程第四篇 一个经典的多线程同步问题>提出了一个经典的多线程同步互斥问题,本篇将用关键段CRITICAL_SECTION来尝试解决这个问题. 本文首先介绍下如何使用关键段,然 ...

  6. Visual studio 2013安装及单元测试

    vs安装过程 单元测试: 创建c#类库 创建单元测试 测试结果

  7. cpio的简单使用

    有如下文件 # file boot.kylin boot.kylin: ASCII cpio archive (SVR4 with no CRC) extract: # cpio -i <boo ...

  8. CDH上执行WordCount的意外和收获

    前面将Cloudera Manager安装到集群上的一台主机后,并通过Cloudera manager安装了hadoop-2.6.0-CDH5.4.4.今日来测试安装的集群是否很够很好的执行mapre ...

  9. hdu1505 dp

    //Accepted 5196 KB 109 ms //类似hdu1506 //输入数据的格式没有明确的限制 //可能出现以下情况 //5 5 //R //F //F F F //F F F F F ...

  10. 深入学习:如何实现不同Android设备之间相同应用程序的网络服务发现功能

    在我们的app中添加网络服务发现功能(NSD)以方便在不同的设备上响应局域网中的请求.这种功能对于多设备之间点对点服务来说很有用,例如多人游戏,多人通话,文件共享等. 一,在网络中注册你的服务 注意: ...