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的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- JQuery+CSS3实现封装弹出登录框效果
原文:JQuery+CSS3实现封装弹出登录框效果 上次发了一篇使用Javascript来实现弹出层的效果,这次刚好用了JQuery来实现,所以顺便记录一下: 因为这次使用了Bootstrap来做一个 ...
- SharePoint使用BCS开发你第一个应用程序(一)
SharePoint使用BCS开发你第一个应用程序(一) 本系列教你使用BCS(Business Connectivity Service)创建OBA(Office business ...
- Sliverlight实例之 绘制扇形和环形图
一,1道几何题 已知两点坐标确定一条直线,直线上存在一个未知点,起始点与未知点的距离已知 求:未知点坐标 思路,如下: 求AB长度,可以根据两点距离公式 二,写个C#类 定义一个Point类,代表坐标 ...
- Linux下安装Oracle11g服务器(转)
安装环境 Linux服务器:SuSe10 sp2 64位 Oracle服务器:Oracle11gR2 64位 系统要求 Linux安装Oracle系统要求 系统要求 说明 内存 必须高于1G的物理内存 ...
- 输出无名空数组---精android、IOS App应用服务程序开发
直接输出 [] 示例文件_samples/app/array_null.json在轻开平台的_samples/app/文件夹下 太Easy.无法写出很多其它的内容,大家还是自己试试吧! ! ! 相关资 ...
- jvm调音
jvm性能调优的目地:1)控制jvm的堆栈大小.比方当你的程序年轻代对象在某个时间段产生的比較多的时候,就须要控制年轻代的堆大小. 同一时候还要控制总的JVM大小避免内存溢出 2)控制GC的行为. G ...
- 返璞归真 asp.net mvc (4) - View/ViewEngine
原文:返璞归真 asp.net mvc (4) - View/ViewEngine [索引页] [源码下载] 返璞归真 asp.net mvc (4) - View/ViewEngine 作者:web ...
- 返璞归真 asp.net mvc (3) - Controller/Action
原文:返璞归真 asp.net mvc (3) - Controller/Action [索引页] [源码下载] 返璞归真 asp.net mvc (3) - Controller/Action 作者 ...
- Everything中文绿色版在Win7/8/10用不了问题的图文教程,只显示盘符
打开Everything后点击菜单上的“工具”——“选项” 勾选 Everything Service 后,点应用 需要用到管理者权限
- 【 D3.js 进阶系列 — 1.1 】 其它表格文件的读取
CSV 表格文件是以逗号作为单元分隔符的,其他还有以制表符 Tab 作为单元分隔符的 TSV 文件,还有人为定义的其他分隔符的表格文件.本文将说明在 D3 中怎样读取它们. 1. TSV 表格文件是什 ...