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的更多相关文章

  1. leetcode解题报告(19):Contains Duplicate II

    描述 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  2. 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 ...

  3. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. 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 ...

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

  7. leetcode解题报告(28):Remove Linked List Elements

    描述 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 ...

  8. leetcode解题报告(27):Reverse Linked List

    描述 Reverse a singly linked list. 分析 一开始写的时候总感觉没抓到要点,然后想起上数据结构课的教材上有这道题,翻开书一看完就回忆起来了,感觉解法挺巧妙的,不比讨论区的答 ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. 网站优化的经验和技巧--精简高效的C#

    对大型网站,技术涉及面非常广,对硬件,软件,编程语言,Web Service,防火墙等等有很高要求.    面对大量用户,高并发请求,可以使用高性能服务器,高性能编程语言,高性能数据库,加大带宽等,这 ...

  2. php 计算一个字符串在另一个字符串中出现的次数

    <?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // ...

  3. WPF中窗口控件的跨线程调用

    在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...

  4. [Cookie] C#CookieHelper--C#操作Cookie的帮助类 (转载)

    点击下载 CookieHelper.rar 下面是代码大家看一下 /// <summary> /// 类说明:CookieHelper /// 联系方式:361983679 /// 更新网 ...

  5. TUXEDO管理命令总结

    tmboot  启动服务: 参数说明: -l  lmid 启动逻辑服务器名为lmcd服务器上的所有进程 -g grpname 启动GROUP名为grpname的所有进程 -i  srvid 启动SRV ...

  6. 使用UIBezierPath和CAShapeLayer画各种图形

    转载自:http://www.cocoachina.com/ios/20160214/15251.html CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画 ...

  7. ios专题 - CocoaPods - 安装

    职业走得很累,停下来,温故技术.顺便开始我得ios博客文章. [原创]http://www.cnblogs.com/luoguoqiang1985 安装 第一步:执行以下命令 sudo gem ins ...

  8. 《转》15种CSS混合模式让图片产生令人惊艳的效果

    浏览器支持 按照现在情况来讲, 浏览器支持 CSSbackground-blend-mode属性还在不断的完善中.早期版本的浏览器目前还不支持,但caniuse.com报告说在Chrome,Firef ...

  9. POJ 2039 To and Fro(模拟)

    To and Fro Description Mo and Larry have devised a way of encrypting messages. They first decide sec ...

  10. 速卖通api--发起授权

    <? $reqURL_onLine = "https://gw.api.alibaba.com/openapi/http/1/system.oauth2/getToken/494739 ...