问题描述如下:

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?

  从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题。

  通过反复思考,我觉得这题类似于追击问题,可以用一个快速遍历指针和一个慢速遍历指针对这个链表进行遍历,如果存在环,那么这个快速指针肯定会在环中某一处追到这个慢指针。尽管有这样的想法,但是在判断出了是否有环之后,该如何进一步获取环的起点呢?个人能力有限,之前没有做过类似的题目,本着学习的目的,于是我查阅了资料,在LeetCode的讨论区里得到了解答:

  设立一个慢指针slow,一个快指针fast,慢指针每次走一步,快指针每次走两步。假设从head到环起点start的距离为x,环的长度为y(即从起点走回起点所需要走的步数)。

  我们知道,当存在环的时候,fast和slow必然会在环中某一处相遇,假设相遇点距离start为m,于是slow和fast在m处相遇时,slow走了:x+ky+m,fast走了:x+ty+m。因为fast走的为slow的两倍,于是:

  ty可以被y整除,所以x+m+2ky应当也能被y整除,即(x+m) mod y=0。于是,可以推断,当二者在m出相遇时,再绕环走x步,便一定可以到达环的起点。

  可得出代码如下:

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(!head) return NULL;
ListNode* slow = head;
ListNode* fast = head;
do{
if(!fast) return NULL;
slow = slow->next;
fast = fast->next;
if(fast) fast = fast->next;
else return NULL;
}while( slow != fast );
slow = head;
while( slow != fast ){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};

[LeetCode]Linked List Cycle II解法学习的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  3. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  4. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  5. [LeetCode] Linked List Cycle II, Solution

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

  6. LeetCode——Linked List Cycle II

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

  7. Leetcode Linked List Cycle II

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

  8. [LeetCode] Linked List Cycle II 链表环起始位置

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

  9. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. java第二课:运算符和表达式

    1.取模%,如果余数为零,则判断可以整除.2.余数永远小于除数.3.自增运算符++或自减运算符--单独使用时,前++.--后++.--效果是一样的4.先加一,后使用,前++:先使用,后加一,后++5. ...

  2. this.Invoke和this.BeginInvoke的区别

    private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...

  3. 基于内嵌Tomcat的应用开发

    为什么使用内嵌Tomcat开发? 开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率. 怎么搭建内嵌To ...

  4. Unity3D开发之NGUI结合粒子系统的遮挡问题

    原地址:http://blog.csdn.net/lihandsome/article/details/22194025 我的是NGUI3.0.3版本,在加入粒子系统的时候发现一直都是在精灵的下面,所 ...

  5. TaskTracker获取并执行map或reduce任务的过程(一)

    我们知道TaskTracker在默认情况下,每个3秒就行JobTracker发送一个心跳包,也就是在这个心跳包中包含对任务的请求.JobTracker返回给TaskTracker的心跳包中包含有各种a ...

  6. Spring MVC 与 web开发

    转载:http://coderbee.net/index.php/java/20140719/959 项目组用了 Spring MVC 进行开发,觉得对里面的使用方式不是很满意,就想,如果是我来搭建开 ...

  7. C++创建一个动态链接库工程

    前话 在我们安装一些软件时,进入软件安装目录会经常看到.dll格式文件,系统system目录也存在许多dll文件 在软件游戏(window平台)更新的时候,很大部分是下载dll文件 所以会好奇这是什么 ...

  8. java:I/O 一行一行读取和写入

    BufferedReader逐行读取 import java.io.*; class Test { public static void main(String args []){ FileReade ...

  9. Android:控件布局(单帧布局)FrameLayout

    FrameLayout:所有控件位于左上角,并且直接覆盖前面的子元素. 在最上方显示的层加上: android:clickable="true" 可以避免点击上层触发底层. 实例: ...

  10. 107. Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...