题目

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?

分析

给你一个链表,要求返回链表中环的开始位置,如果没有环则返回NULL

尽量不占用额外的存储空间

参考牛客网wangxiaobao的回答

  1. 使用快慢指针判断是否有环,如果有环则快慢指针必定会相遇
  2. 定义两个指针分别在链表的开始位置和快慢指针的相遇位置,以相同的速度前进,两指针相遇的位置就是链表中环的开始位置.

证明如下:

  1. X是链表开始位置,Y是环的入口,Z是快慢指针相遇位置.a b c是长度
  2. 两指针在Z处相遇时,快指针走的长度是慢指针的2倍,则有以下等式成立:
2*(a+b) = a+b+n*(b+c)
推出:
a = n*(b+c)-b

根据上面的公式,我们可以让一个指针从X处开始,另一个指针从Z处开始,当第一个指针走了a的距离时,第二个指针刚好回退b的距离,退到环的开始位置和第一个指针相遇

AC代码

/**
* 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) return head;
ListNode * fast = head;
ListNode * slow = head;
//快慢指针相遇,则表示有环
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(fast == slow) break;
}
//如果没有相遇, 返回NULL
if(!fast || !fast->next) return NULL;
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};

leetCodelinked-list-cycle-ii找到链表的环的更多相关文章

  1. 142 Linked List Cycle II(如果链表有环,找到入口结点Medium)

    题目意思:如果有环,返回入口结点 思路:先判断有没环,再计算环的结点数,然后p1指向头,p2往后移结点次数,p1.p2相遇为入口结点 ps:还是利用指针间距这个思路 /** * Definition ...

  2. [算法][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 ...

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

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

  4. 142 Linked List Cycle II 环形链表 II

    给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/proble ...

  5. Leetcode142. Linked List Cycle II环形链表2

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶: 你是否可以不用额外空间解决此题? 方法一:使用map 方法二: 分两个步骤,首先通 ...

  6. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  7. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  8. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  9. [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 ...

  10. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. mysql中建立索引的一些原则

    1.先存数据,再建索引 有索引的好处是搜索比较快但是在有索引的前提下进行插入.更新操作会很慢 2.不要对规模小的数据表建立索引,数据量超过300的表应该有索引:对于规模小的数据表建立索引 不仅不会提高 ...

  2. linux 系统中 /etc/passwd 和 /etc/shadow文件详解

    链接地址:http://blog.csdn.net/yaofeino1/article/details/54616440

  3. 封装一个axios请求后台的通用方法

    import axios from 'axios'; import constant from '@/js/const'; import alert from '@/js/alertView'; le ...

  4. Asp.net Mvc action返回多个模型实体给view

    1.controller中action代码: public class HomeController : Controller { public ActionResult Detail(int id) ...

  5. ubuntu上第一个hello程序

    1.终端中输入gedit  hello.c ,然后输入程序: 2.使用gcc编译器,编译出在PC上运行的hello可执行程序:gcc  ./hello.c  -o   hello-pc; 3.使用ar ...

  6. pymysql 模块 使用目录

    mysql python pymysql模块 基本使用 mysql python pymysql模块 增删改查 插入数据 介绍 commit() execute() executemany() 函数 ...

  7. 技巧:低版本VS打开高版本VS创建的工程

    错误一:当用低版本VS打开高版本VS创建的工程时,会出现: 方案:将该工程的解决方案文件的后缀由xxx.sln改成了xxx.txt然后,查看其内容如下: Microsoft Visual Studio ...

  8. 八种排序算法--java实现(转)

    (转:http://blog.csdn.net/without0815/article/details/7697916) 8种排序之间的关系: 1, 直接插入排序 (1)基本思想:在要排序的一组数中, ...

  9. [QGLViewer]鼠标取点后回调

    纠结的一天:QGLViewer控件重载鼠标事件(AxMapControl类),如何在点击鼠标之后执行一个回调,通知主界面Viewer类执行一个操作. 先是考虑直接使用C风格的回调函数,在AxMapCo ...

  10. 流程控制之if...else

    # #如果:男的年龄>49,那么:小哥哥## age_of_boy = 50# if age_of_boy > 49:# print('小哥哥你好')### # 如果:女人的年龄>3 ...