Linked List Cycle II 题解

题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/


Description

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?

Solution

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == NULL)
return NULL;
ListNode *slow = head, *fast = head;
slow = slow -> next;
fast = fast -> next;
if (fast == NULL || fast -> next == NULL)
return NULL;
else
fast = fast -> next;
while (slow != fast && slow != NULL && fast != NULL) {
slow = slow -> next;
fast = fast -> next;
if (fast == NULL || fast -> next == NULL)
return NULL;
else
fast = fast -> next;
} if (slow != fast)
return NULL; slow = head;
while (slow != fast) {
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
};

解题描述

这道题同样是带环链表系列的题目,是找到链表环的起点。在判断链表是否有环的基础上要增加对环的起点的判断,这就需要搞清楚其中的数学关系:

设链表环起点距离链表头StartLen,链表环的长度为CycleLen,快慢游标第一次相遇的位置距离链表环起点长度为d(不必求出),则有

对慢游标,s1 = StartLen + n * CycleLen + d,

对快游标,s2 = StartLen + m * CycleLen + d

而s2 = 2 * s1

则有 StartLen = (m - 2 * n) * CycleLen - d

所以当第一次相遇之后,将慢游标设为head,然后快慢游标每次只向后移动一个节点,则再次相遇的位置会在链表环的起点

具体参考博客:判断单向链表是否有环,环起点,环长,链表长

[Leetcode Week6]Linked List Cycle II的更多相关文章

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

  2. 【Leetcode】Linked List Cycle II

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

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

  4. [Leetcode Week6]Linked List Cycle

    Linked List Cycle 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/linked-list-cycle/description/ Des ...

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

  6. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  7. (链表 双指针) 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 ...

  8. leetcode 142. Linked List Cycle II

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

  9. 【leetcode】Linked List Cycle II (middle)

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

随机推荐

  1. Django数据模型--表关系(一对多)

    一.一对一关系 使用方法:models.ForeignKey(要关联的模型) 举例说明:年级.教师和学生 from django.db import models class Grade(models ...

  2. ThinkPHP5作业管理系统中处理学生未交作业与已交作业信息

    在作业管理系统中,学生登陆到个人中心后可以通过左侧的菜单查看自己已经提交的作业和未提交作业.那么在系统中如何实现这些数据的查询的呢?首先我们需要弄清楚学生(Student).班级(class).作业提 ...

  3. nodeJs 调试异步程序追踪异步报错

    DeprecationWarning: Calling an asynchronous function without callback is deprecated. 翻译: 不建议在不回调的情况下 ...

  4. poj1200 字符串hash 滚动哈希初探

    假如要判断字符串A“AABA”是否是字符串B“AABAACAADAABAABA”的子串 最朴素的算法是枚举B的所有长度为4的子串,然后逐个与A进行对比,这样的时间复杂度是O(mn),m为A的长度,n为 ...

  5. Python如何进行中文注释

    最近,由于实习工作的需要,开始接触Python,但是第一个大的脚本写下来之后,连中文注释都没办法加,很郁闷,遂在网上找解决办法,在Python 官网上看到这个页面:http://www.python. ...

  6. beta版本冲刺五

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  7. ASP.NET 概述

    https://msdn.microsoft.com/zh-cn/library/4w3ex9c2(VS.100).aspx ASP.NET 概述 更新:2007 年 11 月 ASP.NET 是一个 ...

  8. Isolate-user-vlan技术白皮书

    http://www.h3c.com.cn/Products___Technology/Technology/LAN/Other_technology/Technology_book/200804/6 ...

  9. 【题解】ZJOI2017仙人掌

    感觉这题很厉害啊,虽然想了一天多但还是失败了……(:д:) 这题首先注意到给定图中如果存在环其实对于答案是没有影响的.然后关键之处就在于两个 \(dp\) 数组,其中 \(f[u]\) 表示以 \(u ...

  10. 【考试记录】4.8 Table ( 数论数学 --组合数 & 杨辉三角)

    陆陆续续的开始考很多的试,也会更新这些题目记录下来,免得做完了之后毫无印象,就这么水过去了(以前的考试都是如此,哎……) Table (T1) : 样例: 出于对数学题本能的恐惧考场上放弃了此题专攻T ...