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. 怎样安装Python3

    在浏览器地址栏输入https://www.python.org/ 打开Python官网 好了,安装完成了! 可以把安装路径C:\Users\Administrator\AppData\Local\Pr ...

  2. 讨伐Cucumber行为驱动

    Cucumber行为驱动,简称BDD,其核心思想是把自然语言转换成代码:但在敏捷开发的过程中,这种东西极大的束缚了测试人员的手脚,感觉它像封建时代的八股文,要遵守严格的韵律,反正我个人十分反感:就像在 ...

  3. 07-Mysql数据库----数据类型

    介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考链接:http://www.runoob.com/mysql/mysql-data- ...

  4. ubuntu12.04停留在grub界面问题

    修改ubuntu 12.04 停留在grub界面的步骤: 1. 在/etc/default/grub配置文件中, 添加一项GRUB_RECORDFAIL_TIMEOUT: GRUB_TIMEOUT=2 ...

  5. Python 学习笔记之—— PIL 库

    PIL,全称 Python Imaging Library,是 Python 平台一个功能非常强大而且简单易用的图像处理库.但是,由于 PIL 仅支持到Python 2.7,加上年久失修,于是一群志愿 ...

  6. throw er; // Unhandled 'error' event&Error: ENOENT: no such file or directory,

    今天做一个文件上传的项目时, 用express-formidable往硬盘里面存文件时, 报  ENOENT:no such file or directory 原因就是程序不能像别的语言一样不存在就 ...

  7. w命令集合

    startx:在命令行模式下输入会进入图形界面 exit:注销Linux(以login shell登录会注销账号,以non-login shell登录会退出终端) data:显示日期和时间 data ...

  8. 软工实践Beta冲刺(2/7)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...

  9. DPDK vhost库

    原创翻译,转载请注明出处. vhost库实现了一个用户空间的virtio net server,允许用户直接处理virtio ring队列.换句话说,它让用户可以从VM virtio网络设备读取或写入 ...

  10. 入口文件-npm run dev

    如果你是用vue.js官网提供的脚手架工具并沿用默认配置的话,你执行npm run dev的时候会出来页面,是因为你根目录下的package.json文件里script配置了"dev&quo ...