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

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?

和前面那题类似,只不过这里要找到的是环开始的节点,看下面这张图(图是盗的):

当fast以及slow指针在z处相遇的时候,可以得到一个计算式,关于两个指针走过的距离,有fast指针走过的距离是slow指针的2倍,那么有:

a + b + c + b(fast) = 2 * (a + b)               =>                 a = c;

那么从链表头在选一个指针,和slow指针一起走,正好可以在环的起始点处相遇,代码如下所示:

 /**
* 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) {
if(!head) return NULL;
ListNode * slow = head;
ListNode * fast = head;
while(slow && fast){
slow = slow->next;
fast = fast->next;
if(fast)
fast = fast->next;
if(fast && slow &&fast == slow)
break;
}
if(fast == NULL) return NULL;
ListNode * start = head;
while(slow != start){
slow = slow->next;
start = start->next;
}
return slow;
}
};

还有一篇博客对这个问题还有类似的衍生问题描述的比较清楚,mark一下

LeetCode OJ:Linked List Cycle II(循环链表II)的更多相关文章

  1. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. [LeetCode]141. Linked List Cycle判断循环链表

    快慢指针用来判断循环链表  记住 快慢指针有四种常用的应用场景: 1.找到有序链表的中点,快指针到头的时候,慢指针就是中点. 2.判断是不是循环链表,快慢指针相遇就是 3.找到循环链表的起点,以链表头 ...

  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] 142. Linked List Cycle II 链表中的环 II

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

  5. [LeetCode] 141. Linked List Cycle 链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

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

  7. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  8. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  9. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

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

随机推荐

  1. hadoop开发MapReduce程序

    准备工作: 1.设置HADOOP_HOME,指向hadoop安装目录 2.在window下,需要把hadoop/bin那个目录替换下,在网上搜一个对应版本的 3.如果还报org.apache.hado ...

  2. Pig、Hive、MapReduce 解决分组 Top K 问题(转)

    问题: 有如下数据文件 city.txt (id, city, value) cat city.txt 1 wh 5002 bj 6003 wh 1004 sh 4005 wh 2006 bj 100 ...

  3. JetBrains ReSharper 8.2 Build 8.2.0.2160 && StyleCop

    先安装 StyleCop 再安装 JetBrains ReSharper 8.2 Build 8.2.0.2160

  4. Windows换行符问题

    MAC 和 Windows的换行符不一样,导致有些情况下,MAC编辑的多行文本,在windows的TXT中只是一行. 使用nodepad++可以正确识别出换行符,而且可以将其转为Windows格式,使 ...

  5. cmd命令 启动 和关闭sql服务

    (1)在操作系统的任务栏中单击“开始”菜单,选择“运行”命令,在下拉列表框中输入“cmd”命令,单击“确定”按钮. (2)输入如下命令,即可通过SQL Server命令行启动.停止或暂停的服务. SQ ...

  6. java常见异常(更新中)

    Java的异常分为两种,一种是运行时异常(RuntimeException),一种是非运行异常也叫检查式异常(CheckedException) .NullPointerException 空指针异常 ...

  7. Oracle 集合操作

    在 Oracle 中提供了三种类型集合操作:并(UNION).交(INTERSECT).差(MINUS) · UNION:将多个查询的结果组合到一个查询结果之中,没有重复内容 · UNION ALL: ...

  8. Centos6.5安装python2.7与pip

    安装Python2.7 安装环境 [root@localhost1 ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@local ...

  9. sublime text3配置ctrl+鼠标左键进行函数跳转【转】

    本文转载自:https://blog.csdn.net/shangdibaozi/article/details/77503426 点击Preferences->Browse Packages进 ...

  10. Win10易升是什么?如何彻底关闭Windows10易升?

    很多朋友反馈在使用电脑的时候,突然弹出一个微软Windows10易升的界面,那么Win10易升是什么,怎么样才可以彻底关闭Win10易升呢? win10易升是什么 1.易升是微软官方发布的升级助理或者 ...