题目描述

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.

参考答案

/**
* 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 == NULL || head->next == NULL) return NULL;
ListNode* fp = head;
ListNode* sp = head;
bool isCycle = false; while(fp != NULL && sp != NULL){
fp = fp -> next;
if(sp -> next == NULL) return NULL;
sp = sp->next->next;
if(fp == sp) {
isCycle = true;
break;
}
} if(!isCycle) return NULL; fp = head;
while(fp != sp) {
fp = fp->next;
sp = sp->next;
}
return fp; }
};

答案注释

想象 fast 的速度是一步,slow 的速度是不动。那么,常规情况下,他俩在a点集合,在a点再次相遇。

但由于slow 摸鱼了,晚来了,fast已经走到b点了,slow才和fast集合,一起出发。a-b 就是所求的 循环开始点 到 列表开始点的距离。

更详细解释,可参考:https://www.cnblogs.com/hiddenfox/p/3408931.html

LC 417. Linked List Cycle II的更多相关文章

  1. [LC] 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 II 解题报告

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

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

  4. 15. 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 solve i ...

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

  6. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  7. 【LeetCode练习题】Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  8. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  9. Linked List Cycle && Linked List Cycle II

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

随机推荐

  1. CF1174C Ehab and a Special Coloring Problem(数论)

    做法 与\(x\)互质的数填不同的数,把有向关系表示出来,发现边数是不能承受的 反过来想,成倍数关系填相同的数,把这些数想象成一条链,而这条链开始的数一定是质数,\(\sum\limits_{prim ...

  2. Tkinter 之Scale滑块标签

    一.参数说明 语法 作用 Scale(window, label="滑块") 滑块标题 Scale(window, label="滑块", from_=0) 滑 ...

  3. GitHub的SSH免密连接

    1.进入当前用户的家目录 $ cd ~ 2.删除.ssh 目录 $ rm -rvf .ssh 3.运行命令生成.ssh 密钥目录 $ ssh-keygen -t rsa -C [GitHub邮箱] [ ...

  4. idhttp访问DATASNAP有密码验证的中间件

    idhttp访问DATASNAP有密码验证的中间件 用TIDHttp访问DataSnap Rest服务器,在服务器采用了用户验证的情况下,客户端需要提交密码,否则不能正常连接. procedure T ...

  5. TynSerial类介绍

    TynSerial类介绍 TynSerial是咏南中间件封装的,支持数据二进制序列(还原)的类. 支持WINDOWS.LINUX.MAC.IOS.ANDROID. 支持D6及以上版本. 支持TCP/H ...

  6. 配置 Ubuntu 服务器

    Python: apt install python3-pip sudo add-apt-repository ppa:fkrull/deadsnakes sudo apt-get update ap ...

  7. 转: mysql的取整函数

    一.ROUND()函数用法 ROUND(X) -- 表示将值 X 四舍五入为整数,无小数位    ROUND(X,D) -- 表示将值 X 四舍五入为小数点后 D 位的数值,D为小数点后小数位数.若要 ...

  8. mysql数据库分库分表(Sharding)(转)

    mysql数据库切分 前言 通过MySQLReplication功能所实现的扩展总是会受到数据库大小的限制.一旦数据库过于庞大,尤其是当写入过于频繁,非常难由一台主机支撑的时候,我们还是会面临到扩展瓶 ...

  9. Coherent Calculator

    计算逻辑 输入想要的参数后点击以下按钮进行计算和调整: Formula Bigger N Smaller N Bigger M Smaller M 所以在这个策略中Ft被Fixed在输入的值,其他的三 ...

  10. keras启用tensorboard

    在callback函数中添加tensorboard,启用tensorboard. # TensorBoard callback tensorboard_cb = K.callbacks.TensorB ...