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

Follow up:

Can you solve it without using extra space?

思路:由【Leetcode】Linked
List Cycle
可知。利用一快一慢两个指针可以推断出链表是否存在环路。

如果两个指针相遇之前slow走了s步,则fast走了2s步。而且fast已经在长度为r的环路中走了n圈,则可知:s = n * r。假定链表长为l。从链表头到环入口点距离为x。从环入口点到相遇点距离为a,则:x + a = s = n * r = (n - 1) * r + r =  (n
- 1) * r + l - x。因此x = (n - 1) * r + (l - x - a)。(l - x - a)为从相遇点到环入口的距离,这意味着当一个指针从链表头出发时,还有一个指针从相遇点開始出发,两者一定会在环入口处相遇。

/**
* 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) {
ListNode * slow = head;
ListNode * fast = head; while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next; if(slow == fast)
{
ListNode *slow2 = head;
while (slow2 != slow)
{
slow2 = slow2->next;
slow = slow->next;
}
return slow2;
}
} return NULL; }
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

【Leetcode】Linked List Cycle II的更多相关文章

  1. 【题解】【链表】【Leetcode】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 (middle)

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

  3. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  4. 【Leetcode】【Medium】Linked List Cycle II

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

  5. 【链表】Linked List Cycle II

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

  6. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

  7. 【leetcode】Linked List Cycle

    这个题真是坑死了,只怪自己不好吧.一开始审题,以为是给定一个首尾相连的链表,查看其中是否有循环(原谅我的无知吧!!).然后在那写啊写啊写,还在纠结是局部循环还是前一半和后一半一样这样的循环,blah ...

  8. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  9. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

随机推荐

  1. 第三方插件将数据转成json

    1.需要使用第三方jar commons-beanutils-1.7.0.jar /commons-collections-3.1.jar/commons-lang-2.5jar /commons-l ...

  2. 1、opencv3.3.0和cmake安装步骤(按照以下步骤安装后仅能在PC上运行,动态库也是PC端的属性)

    1.下载安装CMake for Linux 下载地址:https://cmake.org/download/ 我下载的版本是,下载文件cmake-3.11.1.tar.gz ./bootstrap m ...

  3. Java反射机制的简单应用

    一直感觉java的反射机制非常强大,可是可用的地方不多.在android学习的时候.一直想实现挂断电话的功能,可是系统并没有提供开放的api接口,看了一下网上使用反射机制来实现该功能,确实非常强大,非 ...

  4. 嵌入式arm linux环境中gdb+gdbserver调试

    一.前言嵌入式Linux系统中,应用开发过程中,很多情况下,用户需要对一个应用程序进行反复调试,特别是复杂的程序.采用GDB方法调试,由于嵌入式系统资源有限性,一般不能直接在目标系统上进行调试,通常采 ...

  5. 24、驱动调试之printk

    1.uboot跳转到内核启动的时候通过环境变量 console设置控制台 (console = ttySAC0表示输出到串口,并从串口结束输入,也可以设置console=tty1,表示输出到LCD,从 ...

  6. mysql数据库 navicat premium mac 破解教程

    https://www.jianshu.com/p/f3ef78deadaa 转自Navicat Premium for Mac v12.0.22.0 破解教程,macOS上手动破解,无需补丁,无毒 ...

  7. ios开发级联菜单(利用父子控制器--两个菜单封装为两个子控制器来实现)

    一:1:级联菜单可以使用两个tableView来实现,也可以利用父子控制器,两个控制器来实现,根视图控制器作为两个控制器的父控制器,来管理两个子控制器.2:将左右菜单分别交给两个控制器去管理,对于一些 ...

  8. Nginx+uswgi+Django部署

    详情参考: http://blog.csdn.net/a_little_snail/article/details/78045636

  9. SRA解密报错:Data must start with zero

    项目背景:要对打印地址进行加密,用公钥加密后会乱码需要base64 decode一下,但是在解密时报错:javax.crypto.BadPaddingException: Data must star ...

  10. [CSS Flex] Flex direction

    flex-direction: main two 'row' or 'column', you can use reverse also.