LeetCode——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?
原题链接:https://oj.leetcode.com/problems/linked-list-cycle-ii/
题目:给定一个链表。返回环開始的节点。如无环。返回null.
public ListNode detectCycle(ListNode head) {
ListNode fast = head,slow = head;
while(fast != null && fast.next != null){
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;
}
// Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
以下的文章总结得非常好。
学习了。
寻找环存在和环入口的方法:
用两个指针p1、p2指向表头。每次循环时p1指向它的后继,p2指向它后继的后继。
若p2的后继为NULL,表明链表没有环。否则有环且p1==p2时循环能够终止。此时为了寻找环的入口,将p1又一次指向表头且仍然每次循环都指向后继。p2每次也指向后继。
当p1与p2再次相等时,相等点就是环的入口。
參考:http://www.cnblogs.com/wuyuegb2312/p/3183214.html
版权声明:本文博客原创文章。博客,未经同意,不得转载。
LeetCode——Linked List Cycle II的更多相关文章
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- Leetcode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- [C++] WinAES问题
WinAES这是一个很好windows CAPI包. 假设C++项目需求和java程序aes加密和通信的解密,然后WinAES代码是有问题. java的aes默认代码未设置IV和用途ECB模式. 因此 ...
- Eclipse+Maven创建webapp项目<一> (转)
Eclipse+Maven创建webapp项目<一> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...
- Java 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是工厂方法模式的进一步抽象,其英文原话"Provide an interface for creating families ...
- 说说nio2
利不百不变法,功不十不易器 为什么会出现nio,之前的io有什么问题? 请先看 说说nio1 nio类图例如以下 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZX ...
- SRM 628 D1L3:DoraemonPuzzleGame,math,后市展望,dp
称号:c=problem_statement&pm=13283&rd=16009">http://community.topcoder.com/stat?c=probl ...
- STM32本学习笔记EXTI(外部中断)
参考资料:STM32数据表.网络信息 =========================================切割线===================================== ...
- 同台电脑部署多组Tomcat负载均衡(或集群)
可能这种需求比较少见,不过如果手上服务器不够用.可以考虑先这么干着.. 具体Tomcat怎么搭集群,就不在这细说了.只写同台电脑部署多组集群需要修改和注意的地方. 一.Apache 先是Apache, ...
- Convert View To Bitmap
public static Bitmap convertViewToBitmap(View view) { view.destroyDrawingCache(); view.measure(View. ...
- Java模式(适配器型号)
今天阅读Java该适配器模式,这里有一个小的总结和下谈感受.对于将来使用. 首先.让我们有关适配器先说说. 适应是“来源”至“目标”适应.其中连接这两个的关系是适配器.它负责“源”过度到“目标”. 举 ...
- Winform WebBrowser引用IE版本问题
做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...