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?

这个题还是蛮考验数学推理的,不过在前一个题的基础上还是能推出结果的。这是英文一段解释,非常有帮助。

First Step: Assume the first pointer runs from head at a speed of 1-by-1 step, as S, and the second pointer runs at a speed of 2-by-2 step, as 2S, then two pointers will meet at MEET-POINT, using the same time. Define outer loop is A, the distance from CIRCLE-START-POINT to MEET-POINT is B, and the distance from MEET-POINT to CIRCLE-START-POINT is C (Apparently, C=loop-B), then (n*loop+a+b)/2S = (a+b)/S, n=1,2,3,4,5,....

Converting that equation can get A/S=nloop/S-B/S. Since C=loop-B, get A/S = ((n-1)loop+C)/S.

That means, as second step, assuming a pointer running from head and another pointer running from MEET-POINT both at a speed S will meet at CIRCLE-START-POINT;

代码如下:

 public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null || head.next==null) return null;
ListNode pointer1 = head;
ListNode pointer2 = head; while(pointer1!=null && pointer2!=null){
pointer1 = pointer1.next;
if(pointer2.next==null) return null;
pointer2 = pointer2.next.next; if(pointer1==pointer2) break;
}
if(pointer1==null || pointer2==null) return null; pointer1 = head;
while(pointer1 != pointer2){
pointer1 = pointer1.next;
pointer2 = pointer2.next;
}
return pointer1;
}
}

LeetCode OJ 142. Linked List Cycle II的更多相关文章

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

  2. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  3. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...

  4. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

  5. LeetCode OJ:Linked List Cycle II(循环链表II)

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

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

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

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

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

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

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

随机推荐

  1. 如何设置linux的文件和目录的权限

    1 字符表示法 1)chmod [-R] 权限(mode)file   注:[-R]会将目录下所有权限都设置成指定的 who operator permission u(owner) +增加权限 r( ...

  2. B/S、C/S区别

    [B/S.C/S C/S (Client/Server客户端服务器) B/S (Brower/Server浏览器服务器)  区别 1.硬件环境不同: C/S 一般建立在专用的网络上, 小范围里的网络环 ...

  3. LINQ To SQL 处理 DateTime?

    LINQ To SQL 处理 DateTime? 类型 例子: 搜索栏含有最后扫描时间的日期(DateTime?)与多个其他条件(String) 现在需要写一个查询 : 查询符合最后扫描的日期的查询 ...

  4. UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 26269: illegal multibyte sequence

    解决方法参见下面的链接: http://blog.csdn.net/jim7424994/article/details/22675759

  5. iOS开发app上架流程之证书的制作

    1.证书的制作:登陆 https: 1.1appid的注册 选择Identifiers 下的App IDs然后如图所示 点击加号,进入 App ID Description下的Name:这个是appI ...

  6. POJ 2413 How many Fibs?#二分+大数加法

    http://poj.org/problem?id=2413 #include<iostream> #include<cstdio> #include<cstring&g ...

  7. CF 604B More Cowbell#贪心

    (- ̄▽ ̄)-* //把最大单独放,然后第二大的和最小的放一起,第三大的和第二小的放一起 //由此类推,求最大值,即为盒的最小值 #include<iostream> #include&l ...

  8. HDU 1017 A Mathematical Curiosity(枚举)

    题目链接 Problem Description Given two integers n and m, count the number of pairs of integers (a,b) suc ...

  9. [ An Ac a Day ^_^ ] FZU 2030 括号问题 搜索

    FZU一直交不上去 先写在这吧 #include<stdio.h> #include<iostream> #include<algorithm> #include& ...

  10. CEdit实现文本换行

    CEdit控件若要在字符串中插入换行字符("\r\n")实现换行效果,必须指定两个风格 ES_MULTILINE和ES_WANTRETURN. 1: DWORD dwStyle = ...