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 141 Linked List Cycle

性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点

证明:

指针a速度为1 指针b速度为2

记head到环开始处的距离为k,环的周长为r,从环开始处顺方向到相遇点的距离为d

则a到相遇点走过的路程为 Sa = k + d, Sb= k + nr + d (n为b多绕的圈数)

取n= 1 时 Sb = k + r + d 同时要满足 Sa * 2 = Sb

则 d = r - k, Sa = r, Sb = 2r 满足条件

显然 不存在n>1 使得成立的情况

var detectCycle = function(head) {if(!head)return nullvar a = headvar b = headwhile(b.next && b.next.next){b = b.next.nexta = a.nextif(a===b){a = headwhile(a !== b){a = a.nextb = b.next}return a}} return null
}

转载于:https://www.cnblogs.com/lilixu/p/4589910.html

Leetcode 142 Linked List Cycle II相关推荐

  1. Leetcode 142. Linked List Cycle II

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

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

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

  3. [LeetCode] 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 题意:找 ...

  4. Leetcode - 142. Linked List Cycle II

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

  5. LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法

    题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...

  6. 【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 ...

  7. 142. Linked List Cycle II

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

  8. Leetcode 142. Linked List Cycle IIJAVA语言

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

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

    给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 - ...

最新文章

  1. verilog基础-状态机之FPGA独立按键消抖设计与验证(熟练testbench的写法)
  2. 设备底座几个常见固定方式
  3. oracle 安装乱码,linux安装Oracle中文乱码问题汇总
  4. mouseup 左键_javascript中mouseup事件丢失的原因与解决办法
  5. 选择大于努力!0基础学好C语言编程,首先要掌握的是什么?
  6. 数据线为什么不弄两头都是Typec接口的呢?
  7. LeetCode Week 4:第 31 ~ 40 题
  8. python中的对象_Python中的变量、对象
  9. 常用jQuery代码
  10. C语言startup()函数,干货 | 深度剖析C语言的main函数
  11. MFC 键盘鼠标钩子
  12. 不同主机之间通过XDMCP协议通信(X-server和X-client不在同一主机)
  13. 虹软人脸识别 - 基于QT的桌面客户端与微信小程序及服务器设计
  14. 打开.pos文件--ProcessOn
  15. linux media v4l2,Overview of the V4L2 driver framework (v4l2_subdev)
  16. 大道至简:透过现象看本质
  17. HTML中属性问题以及通信问题总结
  18. opencv根据摄像头名称打开摄像头(附源码)
  19. 呸!都TM开始打广告了,垃圾!
  20. 利用Python暴力破解邻居家WiFi密码

热门文章

  1. mosek 安装配置python_python安装、配置以及pyinstaller的安装、使用
  2. vue内检测是否有swiper_vue.js怎么用swiper
  3. typescript索引类型_typescript入门:高级类型
  4. java容器遍历_高效遍历Java容器详解
  5. Redis有序集合详解
  6. Spring Boot 分布式会话
  7. Dev C++详细安装教程
  8. spring boot 如何修改默认端口号和context path
  9. 求数列1/3到1/n之和
  10. python批量音频转格式_python将mp3格式批量转化为wav格式