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?

思路:维护两个指针slow和fast。先判断是否存在环。

在判断是否存在环时,slow每次向前移动一步,fast每次移动两步。

假设从链表头开始一共移动了K次,则fast一共移动了2K步。

两个指针在相遇时,fast一定比slow多跑了至少一个整环。设链表中环长为R,

则有2K - K = nR, 即K = nR。

再看slow所跑过的过程,设链表头到环开始的节点距离为S,环内跑了mR + D,

则有K = S + mR + D。其中,D为slow和fast相遇点在单圈内距离环起点的距离。

与上面比较得 S + mR + D = nR。进一步得 S + D = (n - m)R。

这说明了:在环内,距离环起点D处继续走S步,会得到整数倍的环长,即会回到环起点处。

而巧的是,S正好也是从链表头到环起点处的距离。

因此,我们有了解法:在判断是否存在环这一步中,当slow和fast相遇后,令slow=head,然后两个指针这次都一步一步往前走,这次两者相遇的地方就是环的起点!

算法时间复杂度O(n), 空间复杂度O(1)。

 1 class Solution {
 2 public:
 3     ListNode *detectCycle(ListNode *head) {
 4         if (head == NULL) return NULL;
 5         ListNode* slow = head;
 6         ListNode* fast = head;
 7         while (fast && fast->next)
 8         {
 9             slow = slow->next;
10             fast = fast->next->next;
11             if (fast == slow) break;
12         }
13         if (fast == NULL || fast->next == NULL)
14             return NULL;
15         slow = head;
16         while (slow != fast)
17         {
18             slow = slow->next;
19             fast = fast->next;
20         }
21         return slow;
22     }
23 };

转载于:https://www.cnblogs.com/fenshen371/p/4906617.html

Linked List Cycle II - LeetCode相关推荐

  1. ★ Linked List Cycle II -- LeetCode

    证明单链表有环路: 本文所用的算法 能够 形象的比喻就是在操场其中跑步.速度快的会把速度慢的扣圈 能够证明,p2追赶上p1的时候.p1一定还没有走完一遍环路,p2也不会跨越p1多圈才追上 我们能够从p ...

  2. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

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

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

  4. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  5. leetcode day5 -- Reorder List Linked List Cycle II

    1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...

  6. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...

  7. leetcode: Linked List Cycle II

    http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...

  8. Leetcode 142 Linked List Cycle II

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

  9. Linked List Cycle II

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

最新文章

  1. 显色指数测试软件,显色性
  2. python循环指令_Python循环
  3. 管理角色认知-工程师到管理者角色发生了哪些变化?
  4. Orcale用户管理
  5. 【poj3070】Fibonacci
  6. Dirichlet过程混合模型(DPMM)的Gibbs抽样程序
  7. 11、进入保护模式-V
  8. docker搭建swoole简易聊天室
  9. 深度学习菜鸟的信仰地︱Supervessel超能云服务器、深度学习环境全配置
  10. 我面试了我的前领导,他连做我的下属都不配
  11. java简易计算器程序框图_简易计算器程序设计思路及流程图
  12. Java 找水仙花数
  13. ViewPager翻页动画失效详解
  14. 计算机组成原理笔记——机器指令
  15. Css+Jquery实现点击图片放大缩小预览 图片预览 查看大图
  16. 百度地图和51地图API应用开发
  17. 曹二众 / jeewms仓储管理系统本地部署踩坑记录
  18. MySQL 重置 root 密码以及修改密码时报错password字段不存在
  19. keil5安装及注册许可
  20. goproxy和go modules的初步使用

热门文章

  1. android 揭示动画_遗传编程揭示具有相互作用的多元线性回归
  2. 图像匹配与OpenCV模板匹配
  3. 少数民族青年作家要有更高的标准和目标
  4. Power of Three
  5. sum of two integers
  6. python亲和度_数据挖掘——亲和性分析
  7. js中的json ajax,js结合json实现ajax简单实例
  8. php 实例 规范,PHP开发规范实例详解
  9. php mysql html 转义字符_php, html, javascript, mysql 之间的特殊字符处理
  10. python函数的作用域_python函数的作用域和引用范围