题目描述

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. chrome 截取整个网页

  2. 激活 phpstorm2019.1 win10

    首先添加以下内容到c:\windows\system32\drivers\etc\hosts 文件 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetbrain ...

  3. 三十一、Gawk基础入门

    AWK:Aho Weinberger Kernighan awk :报告生成器.格式化文本输出 一.gawk - pattern scanning and processing language 基本 ...

  4. ios兼容

    border-radius在ios的兼容:-webkit-appearance:none;  加上这个属性,可以保证安卓和ios的圆角一致 上传图片,这段没有代码没有管图片拍摄的方位, var _th ...

  5. hdu6468(记忆化搜索)

    zyb的面试 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  6. java实例化对象的过程

    总结以上内容,可以得到对象初始化过程:  1. 如果存在继承关系,就先父类后子类:  2 .如果在类内有静态变量和静态块,就先静态后非静态,最后才是构造函数:  3 .继承关系中,必须要父类初始化完成 ...

  7. 导入项目后,http://schemas.android.com/apk/res/android报错

    1.复制出现红色字体的路径 2.File - Settings - Language & Frameworks - schemas and DtDs - 粘贴显红路径

  8. Factor_Analysis

    Factor_Analysis(因子分析) Factor Analysis 简书:较好理解的解释,其中公式有一定的推导(仅展现关键步骤,细节大多需要自行补充),基本为结论式. 感性层面理解:首先,明确 ...

  9. LeetCode 560. 和为K的子数组(Subarray Sum Equals K)

    题目描述 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] ...

  10. Python_BDD概念

    BDD概念 全称 Behavior-driven development 中文 行为驱动开发 概念 是敏捷软件开发技术的一种,鼓励各方人员在一个软件项目里交流合作,包括开发人员.测试人员和非技术人员或 ...