题目

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. 数据预处理:标准化(Standardization)

    注:本文是人工智能研究网的学习笔记 常用的数据预处理方式 Standardization, or mean removal and variance scaling Normalization: sc ...

  2. IIS Server Farms入门

    概念了解 IIS Server Farms,实际上应该叫“Microsoft Web Farm Framework (WFF)”,依赖于“Web Platform Installer”才能安装,依赖于 ...

  3. Atcoder681 Typical DP Contest E.数 数位dp

    写什么递归....非递归多好写 令$f[i][j]$表示前$i$位的和在模$d$意义下为$j$的方案数,然后转移即可 复杂度$O(10000 * 100 * 10)$ 注意非递归建议高位摆第$n$位. ...

  4. 某gov的逻辑漏洞

    首先找一个号 在企业信息里面查看到大量的企业名称和组织机构代码 随后去找回密码那 可以看到是直接显示了用户名和密码 随后去登录 可以看到大量的工程信息个企业注册信息 ​

  5. [POI2015]Wilcze doły

    [POI2015]Wilcze doły 题目大意: 给定一个长度为\(n(n\le2\times10^6)\)的数列\(A(1\le A_i\le10^9)\),可以从中选取不超过\(d\)个连续数 ...

  6. HTML的各个标签的默认样式

    head{ display: none } body{ margin: 8px;line-height: 1.12 } button, textarea,input, object,select { ...

  7. alpha冲刺——代码规范、冲刺任务与计划(追光的人)

    代码规范 代码规范整合了自身项目实践还有诸多好的大公司的代码规范.如阿里巴巴开发手册.华为Java规范.W3C前端规范等. 由于内容过于详细和细致,为了方便查看,将其放置在了showDoc网站上(同时 ...

  8. INFORMATION_SCHEMA.COLUMNS-表的字段信息

    当前数据库中当前用户可以访问的每一个列在该视图中占一行.INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.sy ...

  9. Ubuntu16.04配置Android5.0编译环境

    1.安装jdk7 Ubuntu16.04的安装源已经默认没有openjdk7了,所以要自己手动添加仓库,如下: $ sudo add-apt-repository ppa:openjdk-r/ppa ...

  10. HDU 4649 Professor Tian(反状态压缩dp,概率)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...