题目:

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

Follow up:

Can you solve it without using extra space?

让head不断走一步,如果cur不为null,走两步,这样如果存在cycle,cur会追上head。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:bool hasCycle(ListNode *head) {ListNode* cur = head;while (cur && cur->next != head){cur = cur->next;if (cur)cur = cur->next;elsebreak;head = head->next;}if (!cur) return false;return true;}
};

转载于:https://www.cnblogs.com/Doctengineer/p/5838647.html

[Leetcode]141. Linked List Cycle相关推荐

  1. 141. Linked List Cycle

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

  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 python3 简单题141. Linked List Cycle

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百四十一题 (1)题目 英文: Given a linked list, det ...

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

  5. 【LeetCode】详解环形链表141. Linked List Cycle Given a linked list, determine if it has a cycle in it. To

    文章目录 前言 正文 原题: 思路1: 思路2 思路3 总结 前言 今天这道题目的第一种解法很奇葩,用计时器竟然可以AC,并且可以自己调整时间多少,跟我一起来看看吧. 正文 原题: 链接:环形链表 G ...

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

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

  7. LeetCode 141 Linked List Cycle

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

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

  9. [Leetcode]50. Pow(x, n)

    Implement pow(x, n). 我的做法就比较傻了.排除了所有的特殊情况(而且double一般不可以直接判断==),然后常规情况用循环来做.- -||| 直接用循环,时间复杂度就比较大.应该 ...

最新文章

  1. 基片集成波导原理_ETH研究人员开发微型红外光谱仪 可以集成到芯片当中
  2. oracle定时任务失效
  3. 图解windbg查看Win7结构体
  4. 前端性能调优之Yahoo--23条
  5. 1周第1课 Linux 认知、安装 Centos7
  6. H5新人福音~零配置搭建现代化的前端工程
  7. mysql profile 调试sql_SQL Server profile使用技巧
  8. 【JSP开发】有关session的一些重要的知识点
  9. 1000+Redis实例,100+集群,Redis 在海量数据和高并发下的优化实践
  10. 安装LabelMe出现ERROR: No matching distribution found for labelme解决方式(关闭网络代理+使用清华源)
  11. Windows Server AD修改还原模式密码
  12. Spring注解——同一接口有多个实现类,如何注入
  13. 把图片url 伪静态 php,php url伪静态化的实现方法详解
  14. jdbc sql拼接字符串
  15. java中的类图_JAVA类图
  16. NXP LPC芯片程序下载
  17. 传统蓝牙HCI(Bluetooth HCI)的概念介绍
  18. c语言实现snn算法,Orkiszewski算法的C语言实现
  19. 思想实验及其在科学发展中的作用
  20. 【设计模式】策略模式

热门文章

  1. 利用js代码引入其他js文件到页面中
  2. MySql百万数据0秒筛选查询
  3. 如何在vue项目中使用sass(scss)
  4. Jmeter操作之跨线程组传递参数
  5. ssh免密码登陆设置
  6. asp.net mvc kendo Grid Filter
  7. C++关键字--volatile
  8. 实现多个输入框的dialog
  9. [JavaScript] promise中.then()方法
  10. JavaScript(四)—— JavaScript 内置对象/JavaScript 简单数据类型与复杂类型