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. browsersync的安装与基本使用

    browser-sync启动命令 Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面. 官网文档:http://www.brows ...

  2. CSP201503-1:图像旋转

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  3. ZooKeeper的伪分布式集群搭建

    ZooKeeper集群的一些基本概念 zookeeper集群搭建: zk集群,主从节点,心跳机制(选举模式) 配置数据文件 myid 1/2/3 对应 server.1/2/3 通过 zkCli.sh ...

  4. su: Authentication failure

    su: Authentication failure问题解决: su 命令切换失败,提示su: Authentication failure,只要你sudo passwd root过一次之后,下次再s ...

  5. [转载]深入理解Batch Normalization批标准化

    文章转载自:http://www.cnblogs.com/guoyaohua/p/8724433.html Batch Normalization作为最近一年来DL的重要成果,已经广泛被证明其有效性和 ...

  6. kaldi解码及特征提取详解

    目录 1. 注意事项 2. 流程图: 3. 具体流程指令: 1. 注意事项 首先要训练好模型,用到3个文件,分别是: final.mdl(训练模型得到的模型文件) final.mat(用来特征转换) ...

  7. 多线程&&I/O

    不是操作系统的,是UNIX环境高级编程的!

  8. PAT 1090 危险品装箱

    https://pintia.cn/problem-sets/994805260223102976/problems/1038429484026175488 集装箱运输货物时,我们必须特别小心,不能把 ...

  9. 利用vue-cli搭建项目后的目录结构

    npm install -g vue-cli vue init webpack my-project(项目名称)  后的目录结构: -----build         webpack配置相关 --- ...

  10. redis-20180118

    1.redis hash  100% 2.redis list 100% 3.redis sentinel 20%