题意:略.

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

      第一阶段,先用快慢指针找到相遇的节点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. 理解node的模板引擎

    1.1.3:分析模板引擎    1.什么是模板引擎 模板引擎是一个将页面模板和要显示的数据结合生成HTML页面的工具 可以这么理解,如果说Express中的路由控制方法是MVC中的控制器的话,那么模板 ...

  2. JWT 从入门到精通

    什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点 ...

  3. mysql查看执行sql语句的记录日志

    开启日志模式 -- 1.设置 -- SET GLOBAL log_output = 'TABLE';SET GLOBAL general_log = 'ON';  //日志开启 -- SET GLOB ...

  4. Java基础——Oracle(二)

    一.Oracle 中的几个服务 1.OracleDBConsoleorcl 进程:nmesrvc.exe oem控制台服务进程,dba用.Oracle Enterprise Manager(Oracl ...

  5. Ansible安装 入门教程

    learn一门新技术咯: ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置 ...

  6. 初学CSS-2-文本的属性

    文本装饰属性: 格式:text-decoration:underline: 取值:underline(下划线) line-through(删除线) overline(上划线) none(什么都没有) ...

  7. php获取指定月份月初和月末的时间戳

    获取指定月份的开始时间戳和结束时间戳,只需传入年月即可(2018-01,2018-1两种格式都可以) $data['sel_time'] = '2018-11'; $data['begin_time' ...

  8. python基础之数据的三大结构

    python的三大数据结构 1.顺序 2.分支 3.循环 # if语句联系# 如果age小于18岁,则打印信息“未成年”age = 17if age <= 18: print("未成年 ...

  9. 【20181025】win10下Python安装osmnx包

    系统:win10 64位 Python:3.7 在网上查了很多资料,主要有两种方法安装osmnx包,一种是通过anaconda安装,这种方法会自动帮你装好osmnx的依赖包:另一种是用pip安装,需要 ...

  10. css选择器:基本选择器

    基本选择器 1.通用元素选择器 *表示应用到所有的标签. *{ padding:0px; margin:0px; } 2.元素/标签选择器 匹配所有p标签的元素 p{ color:red; backg ...