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

  1. 【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 ...

  2. 【LeetCode OJ】Linked List Cycle

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle/ We set two pointers: the faster poi ...

  3. 【一天一道LeetCode】#141. Linked List Cycle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  5. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

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

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

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

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

随机推荐

  1. C++线程安全的单例模式

    1.在GCC4.0之后的环境下: #include <iostream> using namespace std;template <typename T>class Sing ...

  2. mysql索引类型-形式-使用时机-不足之处--注意事项

    一.索引的类型 1.普通索引   增加 create  index  index_name on table(colume(length));                       例子:cre ...

  3. Java继承多态中的方法访问权限控制

    java中的方法天生具有继承多态特性,这点与C++有很大不同(需要在父类方发上加virtual关键字),但用起来确实方便了许多. 最简单的继承多态 声明一个接口BaseIF,只包含一个方法声明 pub ...

  4. Note_JavaWeb_SpringMVC_尚硅谷

    Spring MVC框架 官网文档 \spring-framework-4.2.6.RELEASE\docs\spring-framework-reference\htmlsingle ------- ...

  5. unity 隐藏GameObject的方法(转)

    改position,移到视野外,推荐,最节省 gameObject.SetActive (false); //要提前引用,要不你就改不回来了... renderer.enabled = false; ...

  6. ios隐藏软键盘

    //判断是否为苹果 var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; // 元素失去焦点隐藏iphone ...

  7. lt&gt&eq

    lt:less than,小于 gt:greater than,大于 eq:equal,等于 le:less equal,小于等于 ge:greater than,大于等于

  8. JavaScript的DOM(文档对象)基础语法总结2

    1.getAttribute()方法,通过元素节点的属性名称获取属性的值. //语法 elementNode.getAttribute(name) //element(元素);Node(节点) //注 ...

  9. 后台验证url是不是有效的链接

    /** * 判断链接是否有效 * 输入链接 * 返回true或者false */ public static boolean isValid(String strLink){ URL url=null ...

  10. scale相关设置—颜色设置

    颜色设置,在R的可视化中,应该算是相对比较重要的一项内容,如何把握颜色,很大程度上影响图形的展现效果. 在ggplot的scale设置中,颜色相关的函数较多: scale_fill/colour_hu ...