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. POJ 1741 树上的点分治

    题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...

  2. POJ 3207 2-sat

    题目大意: 在圆上顺时针n个点,给定m个连接,可以通过圆内或者圆外相交,问能不能找到一种方式,使这些连接的边都不相交 这里很容易看出的是,这些边只有在圆外或者圆内两种连接方式,而且必须选择其中一种 所 ...

  3. mysql 启动错误1026

    进入“事件查看器”“应用程序”果然发现很多MySql的错误Default storage engine (InnoDB) is not available 于是进入MySql的安装目录找到my.ini ...

  4. 通过使用ScriptManager.RegisterStartupScript,呈现后台多次使用alert方法

    在前台HTML中加入alert或者confirm,相信大家已经非常熟悉并且经常使用: <div onclick="alert('hello')">按钮1</div ...

  5. PHP 中的 9 个魔术方法

    这个标题有点牵强因为php有不只9种魔术方法, 但是这些将会引导你使用php魔术方法一个好的开始.它可能魔幻,但是并不需要魔杖. 这些'魔术'方法拥有者特殊的名字,以两个下划线开始,表示这些方法在ph ...

  6. Java---NullPoint经验解析

    tv_yue = (TextView) findViewById(R.id.tv_yue); if(!Constant.cf_balance.isEmpty()){ tv_yue.setText(Co ...

  7. Oracle 中批量导入大量测试数据的方法

    执行如下批处理命令: declare  maxrecords constant int := 100000;  i int := 1;begin  for i in 1 .. 20000 loop   ...

  8. (转)innerHTML、innerText和outerHTML、outerText的区别

    原文:http://walsh.iteye.com/blog/261966 innerHTML.innerText和outerHTML.outerText的区别          博客分类: CSS/ ...

  9. How to Avoid OOM in Android

    1.use java reference(strong soft weak phantom) 2.use android:largeHeap="true" above or VMR ...

  10. hdu 2082

    ps:get到了母函数...看了好久的模板与介绍....似懂非懂..决定要多找些题来试试... 代码: #include "stdio.h" #include "stri ...