Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?


题目标签:Linked List

  题目给了我们一个 Linked List,让我们判断它是否循环。

  利用快,慢指针,快指针一次走2步,慢指针一次走1步,如果循环,快慢指针一定会相遇。

Java Solution:

Runtime beats 98.15%

完成日期:06/09/2017

关键词:singly-linked list;cycle

关键点:fast, slow pointers

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution
13 {
14     public boolean hasCycle(ListNode head)
15     {
16         if(head == null)
17             return false;
18
19         ListNode slow = head;
20         ListNode fast = head;
21
22         do
23         {
24             if(fast.next == null || fast.next.next == null) // if fast reaches to end, it doens't have a cycle
25                 return false;
26
27             fast = fast.next.next; // fast moves 2 steps
28             slow = slow.next; // slow moves 1 step
29
30
31         } while(fast != slow);
32
33         return true; // if fast catches slow, meaning it has a cycle
34     }
35 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

转载于:https://www.cnblogs.com/jimmycheng/p/7883030.html

LeetCode 141. Linked List Cycle (链表循环)相关推荐

  1. [LeetCode] 141. Linked List Cycle 单链表判圆算法

    TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...

  2. 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 ex ...

  3. LeetCode 141 Linked List Cycle

    用快慢指针来判定是否有环. 这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定 ...

  4. Leetcode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

  6. 141. Linked List Cycle

    欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...

  7. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  8. LeetCode 142. Linked List Cycle II--单向链表成环的起点--C++,Python解法

    题目地址:Linked List Cycle II - LeetCode Given a linked list, return the node where the cycle begins. If ...

  9. LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法

    题目地址:Linked List Cycle - LeetCode Given a linked list, determine if it has a cycle in it. To represe ...

最新文章

  1. 专家认为自动驾驶汽车需要很多年的五个原因
  2. 小本创业的11个步骤
  3. VMware中装Win2012并配置Hyper-v
  4. 机器学习的Agile过程
  5. 单片机成长之路(51基础篇) - 022 N76e003 APROM模拟EEPROM驱动
  6. 跨域调用WebApi
  7. 约瑟夫问题的学习(基于循环链表)以及基于循环数组
  8. MAC IOS ssh 连接下修改环境变量
  9. mac删除android sd卡,如何从mac完全删除android及其所有文件?
  10. ctypes python_[python学习之路]ctypes,Python
  11. Pandas set_indexreset_index
  12. 查生日代码_让库克亲自送上生日祝福的10后小学生,还在B站教人学编程?
  13. [原]Jenkins(二十一) jenkins再出发Build periodically和Poll SCM
  14. p51 thinkpad 拆解_ThinkPad P51值得买吗?联想ThinkPad P51移动工作站图解评测
  15. bootstrap在线定制工具
  16. FRM 5.1 现代投资组合理论
  17. javascript写的日历控件(收藏)
  18. [生存志] 第145节 班固著汉书
  19. IDEA 黄色警告 found duplicated code in this file finds duplicated code
  20. 学习Bit Twiddling Hacks

热门文章

  1. Android 关于解决MediaButton学习到的media控制流程
  2. es6 数组去重,数组里面的对象去重
  3. 从svn下载下来的项目遇到的问题
  4. 第六次作业之图形界面
  5. 如何通过方法(函数)来实现两个基本数据类型的数值交换
  6. C++/C学习笔记(九)
  7. CDialog 放到 CDockablePane里,总在外面显示?
  8. jsp网页实现任意进制的数转换成任意进制数
  9. NameNode之DataNode管理
  10. MySQL step by step 安装实战