LeetCode OJ 141. 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?
解决这个问题需要很巧妙的思路,一般我们会想到观察链表中是否出现过重复出现的节点。但是这样的空间的复杂度是O(n),比如用set来存储出现过的节点,然后看下一个节点是否存在于set中。
一个很巧妙的解决办法是设置两个指针,一个快指针和一个慢指针。快指针每次移动两步,慢指针每次移动一步。假设两个指针同时从环的某一个节点出发,环的长度是N。由于快指针每次比慢指针快一步,所以至多N次移动后快指针比慢指针多移动N次,正好超过慢指针一环,也就是说N词移动以后两个指针肯定能相遇。而且不论两个指针是否是从环的同一个节点出发,两个指针都会在M次移动后相遇,且M<=N(M是指指针在环上的移动次数,由于链表中的前半段可能不在环上,所以两个指针到达环上节点的时间会有所不同,快指针会先到达,慢指针后到达)。代码如下:
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while(slow!=null && fast!=null){
slow = slow.next;
fast = fast.next;
if(fast!=null) fast = fast.next;
else return false;
if(slow == fast) return true;
}
return false;
}
}
LeetCode OJ 141. Linked List Cycle的更多相关文章
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode OJ】Linked List Cycle
Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...
- 【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- LeetCode OJ 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【LeetCode】141. Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode OJ:Linked List Cycle(链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
随机推荐
- Unity5权威讲解
Photon Cloud 299c7416-a08d-4a23-95a1-e4be108259aa Shooter 视频:https://pan.baidu.com/s/1kVFJ1x9 项目:htt ...
- 用VB把xlsx转换为xls
Sub Test()Dim wb As Workbook, mPath As String, f As StringApplication.DisplayAlerts = FalseApplicati ...
- Android中的eventBus传值
第一步:在build.gradle中添加依赖dependencies { compile 'org.greenrobot:eventbus:3.0.0'} 第二步:创建一个 Event类: 注意:en ...
- msyql sql语句
参考: http://www.cnblogs.com/aspnethot/articles/1397130.html 修改表字段ALTER TABLE table_name CHANGE old_fi ...
- team viewer - rollback framework could not be initialized
rollback framework could not be initialized, 在安装team viewer 的时候出现的这个错误信息,求大师帮忙 https://zhidao.baidu. ...
- Java中SJBArrayList自己简单实现ArrayList
/** * 自己实现ArrayList * @author zyyt * */ public class SJBArrayList { //存放SJBArrayList中的元素 transient O ...
- thinkphp u 方法
public function test(){ $this->display();echo U('Index/test',array('id'=>1),false,'localhost') ...
- 前台图片上传展示JS(单张图片展示)
<script type="text/javascript"> //下面用于多图片上传预览功能 function setImagePreviews(aval ...
- POJ 3070 矩阵快速幂解决fib问题
矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...
- nginx在linux下的目录结构
配置文件目录 putty 下 whereis nginx /etc/nginx