题目

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. 世界杯:用Python分析热门夺冠球队-(附源代码)

    2018年,火热的世界杯即将拉开序幕.在比赛开始之前,我们不妨用 Python 来对参赛队伍的实力情况进行分析,并大胆的预测下本届世界杯的夺冠热门球队. 通过数据分析,可以发现很多有趣的结果,比如: ...

  2. rabbit:Mnesia could not connect to any nodes

    环境: rabbitmq集群   2台机器,挂了一台后重启服务,发现在服务启动不了错误如下: 这里rabbit连接不商rabbit02这里这个服务也无法启动 解决办法: rabbitmq默认的数据库位 ...

  3. django创建model

    1.model实例 #!/usr/bin/python # coding:utf-8 from __future__ import unicode_literals from django.db im ...

  4. [Luogu5162]WD与积木(多项式求逆)

    不要以为用上Stirling数就一定离正解更近,FFT都是从DP式本身出发的. 设f[i]为i个积木的所有方案的层数总和,g[i]为i个积木的方案数,则答案为$\frac{f[i]}{g[i]}$ 转 ...

  5. BZOJ.3489.A simple rmq problem(主席树 Heap)

    题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...

  6. hdu 4859 最小割

    链接:点我 未懂

  7. 【洛谷】NOIP提高组模拟赛Day2【动态开节点/树状数组】【双头链表模拟】

    U41571 Agent2 题目背景 炎炎夏日还没有过去,Agent们没有一个想出去外面搞事情的.每当ENLIGHTENED总部组织活动时,人人都说有空,结果到了活动日,却一个接着一个咕咕咕了.只有不 ...

  8. TCP的建立和终止 图解

    前言 在没有理解TCP连接是如何建立和终止之前,我想你可能并不会使用connect,accept,close这三个函数并且使用netstat程序来调试应用.所以掌握TCP连接的建立和终止势在必行. 三 ...

  9. CDOJ 838 母仪天下 树状数组 (2014数据结构专题

    母仪天下 Time Limit: 1 Sec  Memory Limit: 162 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/838 Descrip ...

  10. PHP常量定义之define与const对比

    简要归纳PHP中两个常量定义关键字的区别: 1.define是函数,const是语言结构,PHP编译时const速度快.2.define只能用在类外,const类内类外皆可.3.define定义的常量 ...