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

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

判断一个链表是否存在环,维护快慢指针就可以,如果有环那么快指针一定会追上慢指针,代码如下:

 1 class Solution {
 2 public:
 3     bool hasCycle(ListNode *head) {
 4         ListNode * slow, * fast;
 5         slow = fast = head;
 6         while(slow && fast){
 7             slow = slow->next;
 8             fast = fast->next;
 9             if(fast) fast = fast->next;
10             if(fast && slow && slow == fast)
11                 return true;
12         }
13         return false;
14     }
15 };

转载于:https://www.cnblogs.com/-wang-cheng/p/4934642.html

LeetCode OJ:Linked List Cycle(链表循环)相关推荐

  1. Leetcode 142. Linked List Cycle II

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

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

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

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

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

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

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

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

  7. 【leetcode】Linked List Cycle I II

    Given a linked list, determine if it has a cycle in it. Can you solve it without using extra space? ...

  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. 题意:找 ...

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

  10. Leetcode - 142. Linked List Cycle II

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

最新文章

  1. python环境安装opencv,Python环境搭建之OpenCV的步骤方法
  2. iOS LaunchScreen和LaunchImage的转换启动图
  3. python方法重写_python 怎样实现重写
  4. ajax成功或失败的原因,实际上成功后,AJAX发布请求报告失败
  5. Netty入门篇-从双向通信开始
  6. 淘宝万亿级海量交易订单存储在哪?
  7. php编码机器语言,机器语言使用的编码是
  8. 网站选择按钮点击无反应?_Win10系统电脑鼠标左键单击没有反应的解决办法
  9. 想加入一行代码吗?使用code标签
  10. XCode帮助文档离线下载解决办法
  11. kafka入门:简介、使用场景、设计原理
  12. HTML5七夕情人节表白网页制作【生日快乐粒子烟花】HTML+CSS+JavaScript 生日祝福网页代码
  13. Online Judge系统(简称OJ)
  14. 在WPS中提取出的照片在哪找_教你使用 Excel 快速更换照片背景底色
  15. POJ3295 Tautology
  16. linux lpte_PC并行口LPT的IO操作(基于WinIo)
  17. 电脑“应用程序无法启动,因为应用程序的并行配置不正确......“问题的解决方法
  18. NRF51822蓝牙初学笔记1.0之 GAP笔记
  19. IEEE ICIP 2019 | 更快更好的联邦学习:一种特征融合方法
  20. GUI,UGUI,NGUI三种编辑UI界面的插件

热门文章

  1. 『C#基础作业』4.类的静态成员示例
  2. 126 MySQL存储引擎概述
  3. 091 类的多态和多态性
  4. .net core上传
  5. TraceWrite waittype
  6. 新托福考位助手 1.0 Beta2 发布
  7. 在对话框中进行消息映射((CEdit*)GetDlgItem(IDC_EDIT_TEST))-SetWindowText(this is a edit box)...
  8. 前端设计模式(1)--工厂模式
  9. 通过python读取ini配置文件
  10. 虚拟Linux系统使用Windows系统oracle数据库