问题描述

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?

解决原理

以不同的速度去遍历链表,slow指针速度是每步一个结点,fast指针速度是每步两个结点

slow遍历节点数是m,fast遍历节点数是2m

假设链表带环,则slow指针与fast指针一定相遇,因为两指针的相对速度是每步一个节点

假设环的长度是L,则当相遇时,fast遍历的节点数比slow遍历的节点数多NL个,N为正整数

2m = m + NL ——> m = NL,m是环长度的整数倍

相遇时的节点位置与起始点相距m个节点,即相距环长度的整数倍

这样如果令slow指针指向链表起始点,fast指针仍然指向相遇点,并且让slow指针与fast指针以相同的速度遍历链表

则当slow指针指向环的起始点时,因为fast与slow的相对距离是NL,则此时fast必定也指向环的起始位置

所以,当两指针指向同一节点时,此节点即为环的起始点

代码C++

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head;
ListNode *fast = head; while(){
if(!slow)
return NULL;
else
slow = slow->next;
if(!fast->next || !fast->next->next)
return NULL;
else
fast = fast->next->next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
}
}
};

未改进

Q3: Linked List Cycle II的更多相关文章

  1. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  2. 15. Linked List Cycle && Linked List Cycle II

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

  3. Java for LeetCode 142 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 && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. 【LeetCode练习题】Linked List Cycle II

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

  6. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  7. Linked List Cycle && Linked List Cycle II

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

  8. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

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

随机推荐

  1. 四则运算<3>单元测试

    经过分析图一的结果正确,因为输出到文件是为了打印,不要求在线答题功能,因此为实现答题功能. 经过分析,结果正确,满足了选择要求. 选择这六组测试用例的原因是这六组用例将有无乘数法,有无括号,有无负数, ...

  2. OpenHCI - Data Transfer Types

    There are four data transfer types defined in USB(USB中有4种数据传输类型). Each type is optimized to match th ...

  3. ZOJ Problem Set - 3643 Keep Deleting

    题目大意: 给出a和b串,a是b串的子串,如果b串有连续的a串,那么就将b串的a串删除,问删除多少次: 题目分析: 打比赛的时候没敲出来,后来想到用栈的思想去模拟就行,网上还有用KMP+栈去做的,没有 ...

  4. 1130-host ... is not allowed to connect to this MySql server 开放mysql远程连接 不使用localhost

    报错:1130-host ... is not allowed to connect to this MySql server 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在loc ...

  5. Redis的初步安装

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 下载 官网下载:http://redis.io/downlo ...

  6. JavaScript 编写多线程代码引用Concurrent.Thread.js(转)

    这是一个很简单的功能实现: <script type="text/javascript" src="Concurrent.Thread.js">&l ...

  7. 148. Sort List

    Sort a linked list in O(n log n) time using constant space complexity. 代码如下: /** * Definition for si ...

  8. GridView用法大全(转)

    http://www.cnblogs.com/sufei/archive/2010/03/27/1698590.html

  9. poj1417 带权并查集+0/1背包

    题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...

  10. IE6 7 8BUG锦集

    1.浮动元素的双倍margin 说明:这是IE6及其以下版本的一个经典的BUG,触发这个BUG产生的条件是给元素设置了浮动并且同一方向设置了margin值.来看以下代码: <style type ...