Description

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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example

Example 1:

  1. Input: head = [3,2,0,-4], pos = 1
  2. Output: true
  3. Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

  1. Input: head = [1,2], pos = 0
  2. Output: true
  3. Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

  1. Input: head = [1], pos = -1
  2. Output: false
  3. Explanation: There is no cycle in the linked list.

Challenge

Follow up:
Can you solve it without using extra space? (O(1) (i.e. constant) memory)?

Solution

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. bool hasCycle(ListNode *head) {
  12. if(head==NULL) return false;
  13.  
  14. ListNode *fast = head;
  15. ListNode *slow = head;
  16.  
  17. while(true){
  18. // If a node is NULL, there is no cycle.
  19. if(fast->next == NULL || fast->next->next == NULL) return false;
  20.  
  21. slow = slow->next;
  22. fast = fast->next->next;
  23.  
  24. // When the fast and the slow run into the same node, there's a cycle.
  25. if ( slow->val == fast->val )
  26. return true;
  27. }
  28. }
  29. };

[Algorithm] 11. Linked List Cycle的更多相关文章

  1. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  2. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  3. Leetcode 中Linked List Cycle 一类问题

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  4. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  5. LeetCode: Linked List Cycle 解题报告

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. [LeetCode] Linked List Cycle II, Solution

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

  7. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  8. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  9. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

随机推荐

  1. YTU 2631: B1 能存各种类型数据的Store类

    2631: B1 能存各种类型数据的Store类 时间限制: 1 Sec  内存限制: 128 MB 提交: 245  解决: 177 题目描述 有一种类,海纳百川,可以对任意类型的数据进行存取,造就 ...

  2. Java 技术体系(JDK 与 JRE 的关系)、POJO 与 JavaBeans

    Java 技术体系的分层结构(不同的颜色表示不同的层次),尤其注意 JDK 与 JRE 之间的包含关系: 图见 Java Platform Standard Edition 7 Documentati ...

  3. flask装饰器route实现路由功能理解

    利用装饰器的方式实现了路由函数,这是一个十分简单清晰的结构,而这个功能的实现,有着很大的学习意义 @appweb.route('index',methods=['GET','POST'] def st ...

  4. emma中文显示乱码问题解决(ubutnu)

    vim -/.emma/emmarc 找到  db_encoding=latin1 改为  db_encoding=utf8  然后重新运行emma,此时发现还是乱码,不要着急,在执行所有的sql语句 ...

  5. vue 加载文件,省略后缀后的加载顺序

    Vue使用import ... from ...来导入组件,库,变量等.而from后的来源可以是js,vue,json.这个是在webpack.base.conf.js中设置的: module.exp ...

  6. sql server数据库占用cpu太大,使用sys.dm_exec_query_stats查询优化

    查询sql语句占用 CPU详细信息: SELECT (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , ( (CASE WHE ...

  7. bzoj 1629: [Usaco2007 Demo]Cow Acrobats【贪心+排序】

    仿佛学到了贪心的新姿势-- 考虑相邻两头牛,交换它们对其他牛不产生影响,所以如果交换这两头牛能使这两头牛之间的最大值变小,则交换 #include<iostream> #include&l ...

  8. [App Store Connect帮助]六、测试 Beta 版本(4.4) 管理 Beta 版构建版本:停止测试构建版本

    在首页上,点按“我的 App”,选择您的 App,然后在工具栏中点按“TestFlight”. 在左列中的“构建版本”下,点按您 App 的平台(iOS 或 Apple TVOS). 在右表中,点按该 ...

  9. easyui-datebox 年月视图显示

    //年月视图做法 $('#startYearDate').datebox({ onShowPanel: function () { //显示日趋选择对象后再触发弹出月份层的事件,初始化时没有生成月份层 ...

  10. jQuery——表单应用(4)

    HTML: <!--复选框应用--> <!DOCTYPE html> <html> <head> <meta charset="UTF- ...