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

Follow up:
Can you solve it without using extra space?

快慢指针找重合判断是否循环、而后fast从头开始+1,slow继续前进直到重合

思路解析:

首先我们看一张图;

设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。因为fast的速度是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c。让两个指针分别从X和Z开始走,每次走一步,那么正好会在Y相遇!也就是环的第一个节点。
 /**
* 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) {
if(head==NULL)
return NULL;
ListNode *fast=head,*slow=head;
ListNode *res=NULL;
while(fast!=NULL&&fast->next!=NULL){
fast=fast->next->next;
slow=slow->next;
if(fast==slow){
res=fast;
break;
}
}
if(res==NULL)
return res; fast=head;
while(fast!=res){
fast=fast->next;
res=res->next;
}
return res;
}
};

linked-list-cycle-ii——链表,找出开始循环节点的更多相关文章

  1. 142. Linked List Cycle II(找出链表相交的节点)

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

  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之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

  5. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  6. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  8. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

  9. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

随机推荐

  1. python 学习总结5

    字符串类型及操作 一.字符串类型的表示 (1)字符串:由0个或多个字符组成的有序字符序列  例如:“请输入带有符号的温度值” 或者‘c’都是字符串 (2)字符串是字符的有序序列,可以对其中的字符进行索 ...

  2. nrf52810学习笔记——三

    在开发nRF52系列的蓝牙方案的时候,会用到IDE.SDK.softdevice.nrfgoStudio等开发软件,这里做一个小小的总结. 首先,下载SDK,里面有适合keil4号iar7(iar8也 ...

  3. 解决like '%字符串%'时索引不被使用的方法

    解决like '%字符串%'时索引不被使用的方法   分步阅读 解决like '%字符串%'时索引不被使用的方法,如果like以通配符开头('%abc')时索引会失效会变成全表扫描的操作. 工具/原料 ...

  4. Untiy CurvedUI 的使用的bug修正

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51996538 作者:car ...

  5. 浅谈 HTTP 协议

    一. HTTP简介 超文本传输协议 Hyper Text Transfer Protocol 是一种用于分布式.协作式和超媒体信息系统的应用层协议 HTTP是万维网的数据通信的基础 HTTP有很多应用 ...

  6. 二分查找与 bisect 模块

    Python 的列表(list)内部实现是一个数组,也就是一个线性表.在列表中查找元素可以使用 list.index() 方法,其时间复杂度为O(n).对于大数据量,则可以用二分查找进行优化.二分查找 ...

  7. 【javascript面试题】5个经典的面试题

    问题1: 作用域 看一下下面的代码: (function(){ var a = b =5; })(); console.log(b); 结果会输出什么? 答案: 5 这个问题考查的要点是两个不同的作用 ...

  8. Centos6.5搭建git远程仓库

    远程仓库搭建 step1:安装git ```yum -y install git``` step2:创建用户git,用来运行git服务 useradd git passwd git //修改git用户 ...

  9. hdu6069[素数筛法] 2017多校4

    对于[l , r]内的每个数,根据唯一分解定理有   所以有  因为     //可根据唯一分解定理推导 所以      题目要求 就可以运用它到上述公式 (注意不能暴力对l,r内的数一个个分解算贡献 ...

  10. iOS大转盘抽奖

    功能 点击大转盘旋转后固定到某个自己可以确定的位置 结构 转盘,开始按钮,指针 技术 CADisplayLink不停重绘,CGAffineTransform旋转,简单数学公式 核心代码 1.使用CAD ...