问题描述

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. fedora下vim配置

    一.安装 1)dnf安装 fedora22以下 sudo yum install vim fedora22及以上 sudo dnf install vim 如果遇到下面类似的错误 .gz from : ...

  2. JQuery源码分析(三)

    jQuery中ready与load事件 jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //document read ...

  3. Balance_01背包

    Description Gigel has a strange "balance" and he wants to poise it. Actually, the device i ...

  4. CodeForces 414D (贪心)

    problem Mashmokh and Water Tanks 题目大意 给你一棵树,k升水,p块钱,进行一次游戏. 在游戏进行前,可以在任意个节点上放置1升水(总数不超过k) 游戏进行若干轮,每轮 ...

  5. PowerShell 语法结构

    Get-Service -name P* [int]$a = 2 write-output $a [string]$b = "string" write-output $b #$c ...

  6. 六、CCLayer

    一个游戏中可以有很多个场景,每个场景里面又可能包含有多个图层,这里的图层一般就是CCLayer对象.CCLayer本身几乎没什么功能,对比CCNode,CCLayer可用于接收触摸和加速计输入.其实, ...

  7. Linux 常用命令笔记

    Linux 常用命令笔记 1. locate locate:用来定位文件的位置,如:locate a.txt 但是这个命令有延迟,也就是新建的文件不一定能搜索到,如果非要找到新建的文件可以使用 upd ...

  8. 如何创建 C# 控制台应用程序

    [转] 如何:创建 C# 控制台应用程序 本主题旨在生成最简单形式的 C# 程序(控制台应用程序)熟悉 Visual Studio 2008 开发环境.由于控制台应用程序是在命令行执行其所有的输入和输 ...

  9. 解决spring-mvc @responseBody注解返回json 乱码问题

    在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...

  10. ZSDR017-客户订货价格和库存

    *----------------------------------------------------------------------*ZSDR017-客户订货价格和库存*---------- ...