题目

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

Follow up:
Can you solve it without using extra space?

题解

这个连同I都是很经典的题啦,刷CC150时候就折磨了半天。

其实就推几个递推公式就好。。首先看图(图引用自CC150):

从链表起始处到环入口长度为:a,从环入口到Faster和Slower相遇点长度为:x,整个环长为:c。

明确了以上信息,就可以开始做运算了。。

假设从开始到相遇,Slower走过的路程长为s,由于Faster的步速是Slower的2倍,那么Faster在这段时间走的路程长为2s。

而对于Faster来说,他走的路程还等于之前绕整个环跑的n圈的路程nc,加上最后这一次遇见Slower的路程s。

所以我们有:

2s = nc + s

对于Slower来说,他走的路程长度s还等于他从链表起始处到相遇点的距离,所以有:

s = a + x

通过以上两个式子代入化简有:

a + x = nc

a = nc - x

a = (n-1)c + c-x

a = kc + (c-x)

那么可以看出,c-x,就是从相遇点继续走回到环入口的距离。上面整个式子可以看出,如果此时有个pointer1从起始点出发并且同时还有个pointer2从相遇点出发继续往前走(都只迈一步),那么绕过k圈以后, pointer2会和pointer1在环入口相遇。这样,换入口就找到了。

Reference: http://blog.csdn.net/xiaxia__/article/details/19356861

代码如下:

 1     public ListNode detectCycle(ListNode head) {
 2         if(head==null||head.next==null)
 3             return null;
 4         
 5         ListNode fast = head,slow=head;
 6         while (true) {
 7             if (fast == null || fast.next == null) {
 8             return null;   
 9         }
             slow = slow.next;
             fast = fast.next.next;
             
             if(fast==slow)
                 break;
         }
         
         slow = head;//slow back to start point
         while(slow != fast){
             slow = slow.next;
             fast = fast.next;
         }
         return slow; //when slow == fast, it is where cycle begins
     }

Linked List Cycle leetcode II java (寻找链表环的入口)的更多相关文章

  1. [LeetCode] 141&142 Linked List Cycle I & II

    Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...

  2. Linked List Cycle I&&II——快慢指针(II还没有完全理解)

    Linked List Cycle I Given a linked list, determine if it has a cycle in it. Follow up: Can you solve ...

  3. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

  4. Linked List Cycle leetcode java (链表检测环)

    题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...

  5. Leetcode | Linked List Cycle I && II

    一.判断链表是否存在环,办法为: 设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针 ...

  6. Linked List Cycle——LeetCode

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  7. Linked List Cycle - LeetCode

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  8. LeetCode 141. 环形链表(Linked List Cycle) 19

    141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...

  9. 【Leetcode】287. 寻找重复数(数组模拟链表的快慢指针法)

    寻找重复数 根据题意,数组中的数字都在1~n之间,所以数字的范围是小于数组的范围的,数组的元素可以和数组的索引相联系. 例如:nums[0] = 1 即可以将nums[0]作为索引 通过nums[0] ...

随机推荐

  1. react篇章-React State(状态)-数据自顶向下流动

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  2. Oracle数据库脚本中的set define off

    2018年8月6日15:11:34 Oracle数据库脚本中的set define off 前言 最近在公司写需求,接触到脚本,第一句set define off;就不知道什么意思了,查询后记录之. ...

  3. CF582A GCD Table

    A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. socket--多进程,多线程服务器

    一:概念: 我们知道IP地址是标志网络中不用主机的IP地址,而端口号就是同一台主机上标志不同进程的地址,IP地址和端口号标志网络中的唯一地址.(又称socket) 在TCP协议中,建⽴立连接的两个进程 ...

  5. 如何破解安卓App

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com 如何破解安卓App

  6. 2018-2019-2 20162318《网络攻防技术》Exp5 MSF基础应用

    1.实验内容 1.一个主动攻击实践,如ms08_067 2. 一个针对浏览器的攻击,如ms11_050) 3. 一个针对客户端的攻击,如Adobe 4. 成功应用任何一个辅助模块 2.基础问题回答 2 ...

  7. Vue 生命周期方法

    一.Vue 生命周期 Vue的生命周期即是实例从创建到销毁的一个过程.之前在学习Vue的时候,看过官网的教程,但是经常用到的是mounted,所以对其他生命周期方法不是很熟悉,这里有空做个总结,也方便 ...

  8. 细说React(一)

    React 是近期非常热门的一个前端开发框架. 这篇文章将介绍如何使用 React 来创建用户界面,希望能够起到抛砖引玉的效果. "React,  A JAVASCRIPT LIBRARY ...

  9. [转]Android Message.obtain() 和Handler.obtainMessage()的区别

        目录(?)[+]   参考:http://www.2cto.com/kf/201311/255885.html http://www.cnblogs.com/over140/archive/2 ...

  10. Codeforces Round #222 (Div. 1) A. Maze dfs

    A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...