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. JavaScript-4.2函数,变量作用域---ShinePans

    <html> <head> <meta http-equiv="content-type" content="text/html;chars ...

  2. docker镜像制作---jdk7+tomcat7基础镜像

    1. 安装docker rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm y ...

  3. mysql进阶(二)之细谈索引、分页与慢日志

    索引 1.数据库索引 数据库索引是一种数据结构,可以以额外的写入和存储空间为代价来提高数据库表上的数据检索操作的速度,以维护索引数据结构.索引用于快速定位数据,而无需在每次访问数据库表时搜索数据库表中 ...

  4. python16_day10【SelectWeb、SelectWget、paramiko、pymysql】

    一.select实现Web框架(自定义WEB框架) 浏览器输入:http://127.0.0.1:8888/index.html import select import socket class F ...

  5. 解释一下python中的//,%和**运算符

    //运算符执行地板除法(向下取整除),它会返回整除结果的整数部分 print(7//2) 这里整除后会返回3.5 同样的,执行取幂运算,ab会返回a的b次方 print(2**10) 最后,%执行取模 ...

  6. mysql第三天作业

    1.将所有的课程的名称以及对应的任课老师姓名打印出来,如下:SELECT cname,tname FROM course LEFT JOIN teacher ON teacher.tid=course ...

  7. Java集合(3):Vector && Stack

    一.Vector介绍 Vector可以实现可增长的动态对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.不过,Vector的大小是可以增加或者减小的,以便适应创建Vector后进行添加或者删 ...

  8. hadoop单击模式环境搭建

    一 安装jdk 下载相应版本的jdk安装到相应目录,我的安装目录是/usr/lib/jdk1.8.0_40 下载完成后,在/etc/profile中设置一下环境变量,在文件最后追加如下内容 expor ...

  9. oracle 数据泵

      Oracle数据库导入导出工具,可以使用exp/imp,但这是比较早期的工具.本文主要介绍数据泵expdp/impdp工具的使用.   1.建立数据泵目录 使用数据泵需要先建directory c ...

  10. hadoop---Java 网络IO编程总结BIO、NIO、AIO

    转载请注明出处:http://blog.csdn.net/anxpp/article/details/51512200,谢谢! 本文会从传统的BIO到NIO再到AIO自浅至深介绍,并附上完整的代码讲解 ...