题意:略.

这个题最关键的点在于后面,如何找到循环开始的节点。

      第一阶段,先用快慢指针找到相遇的节点C。(至于为什么,了解一下欧几里德拓展解决二元不定方程。)A是表头。B是开始循环的位置。

      第一次阶段的公式是: 2(x+y)=x+y+n(y+z);    注意一下:n表示快指针比慢指针多跑了n圈!

      那么两边同时减去 x+y    则, x+y= n*(y+z);  注意:这里y+z表示一整圈!则 x等于 (n-1)*y+n*z;  (仔细分析这个式子的含义)

      当n等于1时,是不是x=z,当n=2时,是不是相当于先跑一圈,A-B剩下的等于z,依次类推更大的n

      所以,当一个指针放在C点,第二个指针放在A点, 以相同的而速度向前推进时,相等处就是B点

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == NULL) return NULL; //空表
ListNode *slow = head;
ListNode *fast = head;
while (fast&&fast->next){
slow = slow->next; fast = fast->next->next;
if (slow == fast)break; //相遇
}
if (fast == NULL || fast->next == NULL)return NULL;
slow = head;
while (slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};

linked-list-cycle-ii (数学证明)的更多相关文章

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

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

  2. Linked List Cycle && Linked List Cycle II

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

  3. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  4. 【LeetCode】142. Linked List Cycle II (2 solutions)

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  5. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  6. [Leetcode Week6]Linked List Cycle II

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

  7. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  8. 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 y ...

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

  10. 15. 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 solve i ...

随机推荐

  1. MyBatis从入门到放弃五:调用存储过程(SQLServer2012)

    前言 如果是相对于复杂的SQL逻辑我们肯定是基于存储过程开发,这篇学习下执行存储过程,调用存储过程如果参数较多我们可以创建parameterMap. 搭建开发环境 开发环境和上篇文章保持相同 创建存储 ...

  2. C# 快速释放内存的大数组

    本文告诉大家如何使用 Marshal 做出可以快速释放内存的大数组. 最近在做 3D ,需要不断申请一段大内存数组,然后就释放他,但是 C# 对于大内存不是立刻释放,所以就存在一定的性能问题. 在博客 ...

  3. CentOS7日期时间设置方法以及时间基本概念介绍

    在CentOS 6版本,时间设置有date.hwclock命令,从CentOS 7开始,使用了一个新的命令timedatectl. 一.基本概念 1.1 GMT.UTC.CST.DST 时间 (1) ...

  4. (2)Jquery1.8.3快速入门_checkbox全选取消部分选中

    1. jquery示例功能: checkbox多选框 全选 .全不选. 选择部分. 源码 : <!DOCTYPE html> <html> <head> <m ...

  5. css兼容问题(一)

    开头语:不用就忘,还是自己乖乖的记笔记吧! 正文开始:    (一)如果你的页面对IE7兼容没有问题,又不想大量修改现有代码,同时又能在IE8中正常使用,微软声称,开发商仅需要在目前兼容IE7的网站上 ...

  6. polyfill-eventsource added missing EventSource to window ie浏览器 解决方案

    今天遇到一个 ie浏览器显示空白,报错内容是: polyfill-eventsource added missing EventSource to window的问题, import 'babel-p ...

  7. canvas-3radialGradient.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. CSS之fontAwesome代替网页icon小图标

    引言 奥森图标(Font Awesome)提供丰富的矢量字体图标—通过CSS可以任意控制所有图标的大小 ,颜色,阴影. 网页小图标到处可见,如果一个网页都是干巴巴的文字和图片,而没有小图标,会显得非常 ...

  9. Kotlin入门(4)声明与操作数组

    上一篇文章介绍了基本变量类型在Kotlin中的用法,不过这只针对单个变量,如果要求把一组相同类型的变量排列起来,形成一个变量数组,那又该如何声明和操作呢? 在Java中声明数组,跟在C语言中声明是一样 ...

  10. [20171206]rman与truncate2.txt

    [20171206]rman与truncate2.txt --//上午测试发现truncate的表在做rman备份时还要做8个extents的备份.--//不知道自己的猜测是否正确,选择一个使用UNI ...