linked-list-cycle-ii (数学证明)
题意:略.
这个题最关键的点在于后面,如何找到循环开始的节点。
第一阶段,先用快慢指针找到相遇的节点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 (数学证明)的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 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 ...
- 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 ...
- 【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 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- 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 ...
- 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 ...
- [算法][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 ...
- 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 ...
随机推荐
- centos每天自动备份mysql数据库
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin export PATH #数据库用户名 db ...
- VirtualBox安装CentOS7
一:.下载CentOS7的镜像 下载地址:https://www.centos.org/download/ 进入后有三个版本可以选择: 1.DVD ISO 标准安装版,一般下载这个就可以了(推荐)本 ...
- Ado.net怎么执行存储过程?
与ADO.Net执行SQL语句的地方只有两点不同1.使用存储过程名代替sql语句2. 使用查询对象SqlCommand,需配置一个CommandType属性 存储过程的执行语法-> exec 存 ...
- [PHP] 算法-复制复杂链表的PHP实现
复杂链表的复制: 1.在旧链表中每个结点的后面复制出一个结点,隔代 2.把旧链表的随机指向部分,复制到新添加的结点上 3.把新结点从旧链表中拆分出来成新链表 1. linklist=head whil ...
- 大数据之 Spark
1 渊源 于2009由Matei Zaharia创立了spark大数据处理和计算框架,基于内存,用scala编写. 2 部署 2.1 需要软件包 下载路径见已有博文 Jdk ——因为运行环境为jvm ...
- 通过写一个Demo展示C#中多种常用的集合排序方法
不多说,程序很简单,就是将集合中的数据进行排序,但使用到的知识点还是比较多的,大牛勿喷,谨献给初学者!直接上程序吧! namespace Demo { /// <summary> /// ...
- Navicat安装及简单使用
一.安装 下载完之后,直接解压出来就能用,看一下解压之后的目录: 双击打开下面这个文件(可以把它添加一个桌面快捷方式,或者添加到任务栏): 然后会提示你输入注册码: 回到navicat的解压出来的文件 ...
- 消除2个按钮之间1px细节引起的冲突
1.代码 <!doctype html> <html lang="en"> <head> <meta charset="UTF- ...
- 从零开始学习html(二)认识标签(第一部分)——上
一.语义化,让你的网页更好的被搜索引擎理 学习html标签过程中,主要注意两个方面的学习:标签的用途.标签在浏览器中的默认样式. 标签的用途:我们学习网页制作时,常常会听到一个词,语义化. 那么什么叫 ...
- NFS共享权限挂载
mount -t nfs 192.168.2.203:/data/lys /lys -o proto=tcp -o nolock mount 172.16.2.18:/home/arcgisserve ...