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. Android(Lollipop/5.0) Material Design(一) 简单介绍

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  2. poj 2240 floyd算法

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17349   Accepted: 7304 Descri ...

  3. WCF学习笔记——WCF基础

    一 WCF与SOA SOA是一种通过为所有软件提供服务外观,并将这些服务的WSDL集中发布到一个地方的一种组织企业软件的方法.它通过使用明确定义的接口通过跨越边界传递消息来让多个自治的服务协同工作.S ...

  4. amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse

    amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse 一.总结 注意点: 1.data-am-collapse:这个东西就是展开折叠事件 2.am-collapse(包括其下属 ...

  5. C语言深度剖析-----函数

    认清函数的真面目 函数的意义 面向过程的程序设计 函数声明和定义 函数参数 编写代码的时候,不要编写类似先后调用的代码 f(k,k++) C语言中的顺序点 a--&&a  ,& ...

  6. 魔兽争霸war3心得体会(一):UD的冰甲蜘蛛流

    玩war3好几年了,之前都是打打电脑,随便玩玩的.刚刚在浩方等平台上和人玩的时候,各种被虐,很难赢一局.从去年开始,才认真玩.思考下各种战术. 最初,使用的是兽族orc,后来觉得兽族不够厉害,玩到对战 ...

  7. wikioi 1051哈希表

    题目描写叙述 Description 给出了N个单词,已经按长度排好了序.假设某单词i是某单词j的前缀,i->j算一次接龙(两个同样的单词不能算接龙). 你的任务是:对于输入的单词,找出最长的龙 ...

  8. stm32 DMA+timer+DAC

    是有延迟的:

  9. (三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1

    原文:(三)RabbitMQ消息队列-Centos7下安装RabbitMQ3.6.1 如果你看过前两章对RabbitMQ已经有了一定了解,现在已经摩拳擦掌,来吧动手吧! 用什么系统 本文使用的是Cen ...

  10. 《大型网站技术架构》1:概述 分类: C_OHTERS 2014-05-07 20:40 664人阅读 评论(0) 收藏

    参考自<大型网站技术架构>第1~3章 1.大型网站架构演化发展历程 (1)初始阶段的网站架构:一台服务器分别作为应用.数据.文件服务器 (2)应用服务和数据服务分离:三台服务器分别承担上述 ...