LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II
1题目
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Linked List Cycle II
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?
2思路
2.1 对于判断链表是否有环
用两个指针,一开始都指向头结点,一个是快指针,一次走两步,一个是慢指针,一次只走一步,当两个指针重合时表示存在环了。
证明:假设链表有环,环的长度为N,慢指针在起始位置,快指针在位置k(位置从0开始计数),那么快指针只要比慢指针多走经过N-k步,就可以追上慢指针了。,因为每一次快指针都比慢指针多走一步,所以一定可以在有限的步数追上慢指针。
2.2如何求出环的起始位置
结论:当快指针和慢指针重合的时候,把一个指针重新指向头指针,两个指针现在速度一样,一次走一步,那么当两个指针值相同时,所在的指针就是环的起始位置。
证明:假设头指针到环的其实位置的长度是k,环的长度是loop,两个指针相遇的时候,慢指针距离还的起始位置的长度是m。
计算:因为快指针的速度是慢指针的两倍。可以推断出:
distance(快指针) = 2 * distance(慢指针),即
k + loop + m = 2 * ( k + m ) ,化解得出
loop = k + m
分析起始点的位置:通过慢指针继续走loop - m步就可以到达环的起始位置,正好k=loop - m,所以,相遇时把快指针指向头指针,两个指针以相同的速度走k步就可以一起到达环的起始位置了。
可以看看这篇文章的解释,看着像我邮学长的blog
3 代码
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
boolean hasCycle = false;
while (fast != null && (fast.next != null)) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
hasCycle = true;
break;
}
}
// find the start of cycle
if (!hasCycle) {
return null;
}
for (fast = head; fast != slow;) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
LeetCode解题报告:Linked List Cycle && Linked List Cycle II的更多相关文章
- leetcode解题报告(19):Contains Duplicate II
描述 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- leetcode解题报告(22):Two Sum II - Input array is sorted
描述 Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- leetcode解题报告(28):Remove Linked List Elements
描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 ...
- leetcode解题报告(27):Reverse Linked List
描述 Reverse a singly linked list. 分析 一开始写的时候总感觉没抓到要点,然后想起上数据结构课的教材上有这道题,翻开书一看完就回忆起来了,感觉解法挺巧妙的,不比讨论区的答 ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- Android(java)学习笔记177:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
国内的主流网络公司(比如网易.腾讯.百度等等),他们往往采用数据挖掘技术获取用户使用信息,从而采用靶向营销.比如电脑上,我们浏览网页的时候,往往会发现网页上会出现我们之前经常浏览内容的商 ...
- DedeCMS文章标题前增加所属栏目名称链接
问题描述:在调用文章标题的时候,我想实现这样的形式:“[国内新闻]站长无忧真的是一个不错的站”,其中“国内新闻”是标题的所属栏目,并且加链接: 解决方法: [field:typelink /]这 ...
- P2P金融的概念理解
P2P金融又叫P2P信贷.其中,P2P是 peer-to-peer 或 person-to-person 的简写, 意思是:个人对个人. P2P金融指个人与个人间的小额借贷交易,一般需要借助电子商务专 ...
- windows下安装wamp和wordpress
安装wamp WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过 ...
- 第4条:多用类型常量,少用#define预处理指令
定义常量的几种方式: 1.#define ANIMATION_DURAION 0.3 //定义了一个动画时长的常量, 预处理过程会把碰到的所有ANIMATION_DURAION一律替换 ...
- iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)
解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...
- C# 控制台程序 托盘图标 事件响应
static void Main(string[] args) { NotifyIconHelper ni = new NotifyIconHelper(); NotifyIconHelper.Sho ...
- 批量翻转PNG图片
用了好几个软件都不好用. 要么不能翻转PNG, 要么翻转之后没有透明度了. 基本上全是图形界面, 要鼠标批量拖放. 所以, 还是自己动手, 写一个批量png翻转工具. #include <ios ...
- [LeetCode OJ] Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Windows phone 之常用控件
一.TextBox TextBox 显示和编辑单格式.多行文本的控件 将TextWrapping的特性设置为Wrap会使文本在到达TextBox控件的边缘时换至新行.必要时会自动扩展TextBox以便 ...