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. 【转】怎么把本地项目和远程git仓库相连通

    1. 打开在你的项目文件夹,输入下面的命令 git init 输完上面的命令,文件夹中会出现一个.git文件夹,如下图所示,其他的的文件也会出现蓝色小问号的标志 2. 添加所有文件 git add . ...

  2. MySQL之单表查询、多表查询

    一.单表查询: 单个表的查询方法及语法顺序需要通过实际例子来熟悉 先将表数据创建下: mysql> create database singe_t1; # 建个数据库singe_t1 Query ...

  3. LeetCode答案(python)

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  4. Mac VMware fusion nat 外网映射

    当我们在使用VMware fusion NAT模式时,相当于形成了一个虚拟的局域网VLAN,这时虚拟机可以对外通信,但是nat对外隐藏了内网,外网访问虚拟机的时候就会遇到问题,比如ping ,ssh ...

  5. HDU 3947 River Problem

    River Problem Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ...

  6. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  7. hihoCoder #1471 拥堵的城市

    这道题目是hihoCoder Challenge 27的C题,我考虑了5天:(. 计数问题.由于树的结构的特殊性(树具有递归结构),不难想到思路是树形DP.由于这是[计数问题]而非[优化问题],我们思 ...

  8. [BZOJ1592] [Usaco2008 Feb]Making the Grade 路面修整(DP)

    传送门 有个结论,每一个位置修改高度后的数,一定是原来在这个数列中出现过的数 因为最终结果要么不递增要么不递减, 不递增的话, 如果x1 >= x2那么不用动,如果x1 < x2,把x1变 ...

  9. Longge的问题(bzoj 2705)

    Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...

  10. keepalived学习

    HA集群 keepalived heartbeat corosync cman 功能实现 vrrp协议在Linux主机上以守护进程方式, 能够根据配置文件自动生成ipvs规则 对各RS健康状态检测 组 ...