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 ...
随机推荐
- 兼容 console 没删除引起 低级浏览器 报错问题
/*重写connsole.log,防止调试代码出错*/if (!window.console){ var names = ["log", "debug", &q ...
- sql 查看语句的性能
SET STATISTICS TIME:看cpu时间 SET STATISTICS IO:关注scan count(计数)------查询读取的表数量:logical read( 逻辑读)次数
- ArcGIS导出辖区边界点坐标
1.使用ArcGIS打开.mxd地图文件 2.选择[Geoprocessing][ArcToolbox] 3.展开菜单,选中如图所示菜单,双击打开 4.选择对应的辖区节点 5.点击OK后,会在左 ...
- “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确
引用sqlite的程序集时,有时会报如下异常: "System.BadImageFormatException"类型的未经处理的异常在 PurchaseDevices.Acces ...
- HDU 5821 Ball
记录一下每个位置最终到达的位置.然后每次操作排序. #pragma comment(linker, "/STACK:1024000000,1024000000") #include ...
- sublime text3编译运行C,Java程序的一些配置
环境:linux 64位 桌面环境: gnome Java编译运行 (1)Preferences --> Browse Packages --> 在该文件夹下新建build文件如: Myj ...
- POJ 2977 Box walking
题目链接:http://poj.org/problem?id=2977 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 222 ...
- Android onConfigurationChanged的作用
API原文说明: android:configChangesLists configuration changes that the activity will handle itself. When ...
- 对低、高频系数直接重构upcoef2
此函数可对原图低.高频系数(或处理后的系数)进行重构 clear all;close all;clc; I=imread('C:\Users\Jv\Desktop\wenli.jpg'); gray= ...
- hdu 1072 广搜
路径是可以重复走的,但是如果再一次走过时间重置点是没有意义的 #include <iostream> #include <cstdio> #include <cstrin ...