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

时间:2022-09-27 21:30:00

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.

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

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.

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

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

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

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

类似题目:

Linked List Cycle

Find the Duplicate Number

参考资料:

https://leetcode.com/problems/linked-list-cycle-ii/

https://leetcode.com/problems/linked-list-cycle-ii/discuss/44793/O(n)-solution-by-using-two-pointers-without-change-anything

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Linked List Cycle II 单链表中的环之二的更多相关文章

  1. [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 ...

  2. [算法][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 ...

  3. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

  4. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  5. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  6. [LeetCode] Linked List Cycle 单链表中的环

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

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

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  8. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  9. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

随机推荐

  1. eclipse maven update error 解决方法

    eclipse  maven  update error 解决方法     本来真不想写这篇博文的,但是eclipse和maven真的是太操蛋了,动不动就出了一些乱七八糟的问题,记录一下.希望公司能早 ...

  2. goprotocbuf的安装和使用

    首先得到 protobuf 相应的包文件 ,在终端上输入如下 wget http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz 下载完毕后 ...

  3. HttpClient Post Form data and get Response String

    DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpost = new HttpPost("http:/ ...

  4. Java+7入门经典 -2 数据

    第2章 程序,数据,变量和计算 2.1 数据和变量 变量是一段有名字的内存, 存储程序中的信息, 描述事物的数据项; 每段定义了名字的内存只能存储一种特定类型的数据. Type; 编译器会检测错误的类 ...

  5. 使用纯css代码实现div的“回”字型“叠放”效果

    正如大家所知道的那样,div是一个块级元素,也是网页编写过程中使用频率最高的一个元素,通过不同的样式控制可以实现一些最常见的页面效果,当然也可以实现一些比较复杂的页面效果,这里就展示一个本人面试过程中 ...

  6. Python基础---python中的异常处理

    Python中的异常处理 一.什么是异常处理 python解释器检测到错误,触发异常(也允许程序员自己触发异常) 程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,与异常处理有关) ...

  7. 转载 USB固件分析

    http://1438431234.spaces.eepw.com.cn/articles/article/item/114022 0x00000000 0x0001fff0 大小 0x1fff1 = ...

  8. CSS3之3D立方体效果

    下面代码可实现3D立方体,比较好理解,就是让每个面先平移到指定位置,然后旋转90度 <!DOCTYPE html> <html lang="en"> &lt ...

  9. Eclipse无法使用springboot2&period;x

    <!-- 阿里云提供的镜像地址 --> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mir ...

  10. Python3:sqlalchemy对sybase数据库操作,非sql语句

    Python3:sqlalchemy对sybase数据库操作,非sql语句 # python3 # author lizm # datetime 2018-02-01 10:00:00 # -*- c ...