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. Spring MVC(二)

    spring mvc工作流 1A)客户端发出http请求,只要请求形式符合web.xml 文件中配置的*.action的话,就由DispatcherServlet 来处理. 1B)Dispatcher ...

  2. Java代码之输出参数和(强制类型转换)

    说明(因为Java中java Application的参数都是默认的字符型的数据,所以需要强制类型转换这一步骤) 设计思想: 向系统里输入若干个参数,计算出参数个数,利用for语句计算出参数的和.(程 ...

  3. selenium grid的使用与配置

    一.selenium grid的组成与作用:由一个集线器hub和多个客户机node组成,如果你的程序需要在不用的浏览器,不同的操作系统上测试,而且比较多的case需要多线程远程执行,那么一个比较好的测 ...

  4. jquery学习笔记2——jq效果

    一.显示隐藏: 可以使用show()和hide()方法来显示隐藏: $("#hide").click(function(){ $("p").hide(); }) ...

  5. 使用PHP连接redis后,timeout连接太多的解决方案

    这个问题,大家在使用php redis之后肯定都会遇到.所以本菜本着虚心求教的原则,又在网上四处求教.得到的答案,无非是以下两种: 1.redis没有主动close. 事后发现,这个答案纯属以讹传讹, ...

  6. HDU 5834 Magic boy Bi Luo with his excited tree

    树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...

  7. linux jdk环境变量

    export JAVA_HOME=/usr/share/jdk8 export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/ ...

  8. Quickly Start Listener scripts

    #!/usr/bin/python # # StartListener.py # Simple python script to start a Meterpreter Listener # Auto ...

  9. /tmp 和 /var/tmp 的区别

    /tmp is meant as fast (possibly small) storage with a short time to live (TTL). Many systems clean / ...

  10. git 和 svn

    GIT和SVN之间的五个基本区别 如果你在读这篇文章,说明你跟大多数开发者一样对GIT感兴趣,如果你还没有机会来试一试GIT,我想现在你就要了解它了. GIT不仅仅是个版本控制系统,它也是个内容管理系 ...