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. 吴超老师课程--Hbase介绍和伪分布式安装

    1.HBase(NoSQL)的数据模型1.1 表(table),是存储管理数据的.1.2 行键(row key),类似于MySQL中的主键.     行键是HBase表天然自带的.1.3 列族(col ...

  2. 算法:LRU(最近最少使用)

    算法:LRU(最近最少使用) 本文参考自小灰文章:https://mp.weixin.qq.com/s/B5xiVeW22ZumbI9KfrYJSg LRU算法 什么是LRU算法 LRU算法又称最近最 ...

  3. redis 笔记05 Sentinel、集群

    Sentinel 1. Sentinel只是一个运行在特殊模式下的Redis服务器,它使用了和普通模式不同的命令表,所以Sentinel模式能够使用的命令和普通的Redis服务器能够使用的命令不同. ...

  4. React setState更新数组中的某个元素Element item

    var items = this.state.items; items[i].status = 'doing'; this.setState({ items: items }); //this.sta ...

  5. MySQL-5.7密码策略及用户资源限制

    1.密码策略 在mysql 5.6对密码的强度进行了加强,推出了validate_password 插件.支持密码的强度要求. (1)安装插件 [root@localhost ~]# ll /usr/ ...

  6. 运维必备技能 WEB 日志分析

    文章节选自<Netkiller Monitoring 手札> 20.2. Web 20.2.1. Apache Log 1.查看当天有多少个IP访问: awk '{print $1}' l ...

  7. 20145222黄亚奇《网络对抗》 逆向及BOF进阶实践学习总结

    20145222<网络对抗> 逆向及BOF进阶实践学习总结 实践目的 1.注入shellcode 2.实现Return-to-libc攻击 知识点学习总结 Shellcode实际是一段代码 ...

  8. QMesageBox的使用

    一.使用构造函数弹出对话框 1. QMessageBox msgBox://最简单的对话框,里面什么也没有 QString str = “test”: msgBox.setText(str); msg ...

  9. com.android.tools.build:gradle:X.XX.XX:gradle.jar 插件无法下载问题

    在使用Android Studio 这个IDE时,出现com.android.tools.build:gradle:X.XX.XX:gradle.jar 插件无法下载问题 可能的原因就是网速不好或者依 ...

  10. 混合开发的大趋势之 一个Android程序员眼中的 React.js 块级作用域 和 let

    转载请注明出处:王亟亟的大牛之路 最近都有事干然后,快到月底了这个月给CSDN的博文也就两篇,想想也蛮多天没更了,那就来一篇. 老规矩,先安利:https://github.com/ddwhan012 ...