[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 represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it without using extra space?
这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参之前那道 Linked List Cycle。这里还是要设快慢指针,不过这次要记录两个指针相遇的位置,当两个指针相遇了后,让其中一个指针从链表头开始,一步两步,一步一步似爪牙,似魔鬼的步伐。。。哈哈,打住打住。。。此时再相遇的位置就是链表中环的起始位置,为啥是这样呢,这里直接贴上热心网友「飞鸟想飞」的解释哈,因为快指针每次走2,慢指针每次走1,快指针走的距离是慢指针的两倍。而快指针又比慢指针多走了一圈。所以 head 到环的起点+环的起点到他们相遇的点的距离 与 环一圈的距离相等。现在重新开始,head 运行到环起点 和 相遇点到环起点 的距离也是相等的,相当于他们同时减掉了 环的起点到他们相遇的点的距离。代码如下:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (!fast || !fast->next) return NULL;
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};
讨论:单链表中的环的问题还有许多扩展,比如求环的长度,或者是如何解除环等等,可参见网上大神的这个总结贴。
Github 同步地址:
https://github.com/grandyang/leetcode/issues/142
类似题目:
参考资料:
https://leetcode.com/problems/linked-list-cycle-ii/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 142. Linked List Cycle II 单链表中的环之二的更多相关文章
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [算法][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 ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- [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 ...
- 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 ...
- (链表 双指针) 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 ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
随机推荐
- tensorflow之tf.shape()
tf.shape()这个方法就相当于numpy当中shape属性. 下面通过列子来了解: 具体而言,tf.shape是用来获取张量的维度(shape).
- 专栏《Elasticsearch 7.x从入门到精通》的相关源代码
新版Elasticsearch 7.3 和 Spring Boot 2.1.7 集成演示项目 第一个项目:演示Elasticsearch 6.4.3 和Spring Boot 2.1.7集 ...
- AI与数学笔记之深入浅出的讲解傅里叶变换(真正的通俗易懂)
原文出处: 韩昊 # 作 者:韩 昊 # 知 乎:Heinrich # 微 博:@花生油工人 # 知乎专栏:与时间无关的故事 # 谨以此文献给大连海事大学的吴楠老师,柳晓鸣老师,王新年老师以及张 ...
- Wine添加路径PATH办法
使用wine运行某些程序时,可能会提示某些DLL找不到,需要手动把这些DLL的路径添加进去.添加方法是:wine regedit打开注册表工具:添加一个键HKEY_CURRENT_USER/Envir ...
- XtraReport报表入库单数字转中文大写数字
先看看打印入库单的效果图,看如下: 客户要求合计一行,要求大写中文数字.XtraReport报表是如何做出以上图的效果呢?因为是要把数字转成大写中文数字,得先准备数字转大写中文数字的函数.因网上有很多 ...
- rsync性能终极优化【Optimize rsync performance】
前言 将文件从一台计算机同步或备份到另一台计算机的快速简便的方法是使用rsync.我将介绍通常用于备份数据的命令行选项,并显示一些选项以极大地将传输速度从大约20-25 MB / s加快到90 MB ...
- Python基础17
写出来的代码,若有部分不想运行,可注释掉. 看跑出来的结果,再加进来调试.
- 云顶之弈换中立python脚本
import pynput keyboard = pynput.keyboard.Controller() mouse = pynput.mouse.Controller() def on_relea ...
- vue学习指南:第五篇(详细) - vue的 computed、methods、watch 的区别?
Computed 计算属性 1. 将函数代码块中返回的结果 赋值 给前面的方法名 2. computed 中的属性有缓存功能,只要data中的数据不发生改变,计算得到的新属 性就会被缓存下 ...
- sparkSQL中的example学习(1)
SparkSQLDemo.scala import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.sql.types ...