用快慢指针来判定是否有环。

这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定在中点。

 1 public class Solution {
 2     public boolean hasCycle(ListNode head) {
 3         if (head == null || head.next == null) {
 4                 return false;
 5         }
 6         ListNode fast = head;
 7         ListNode slow = head;
 8         while (fast.next != null && fast.next.next != null) {
 9             slow = slow.next;
10             fast = fast.next.next;
11             if (fast == slow) {
12                 return true;
13             }
14         }
15         return false;
16     }
17 }

转载于:https://www.cnblogs.com/mayinmiao/p/8488245.html

LeetCode 141 Linked List Cycle相关推荐

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

  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 单链表判圆算法

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

  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. 141. Linked List Cycle

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

  6. Leetcode 142. Linked List Cycle II

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

  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. python应用程序类型_python – 类型提示条件可变参数应用程序
  2. 改写教科书!Science揭示:为什么你体内的癌细胞没发展成癌症?
  3. SSM框架笔记15:SSM框架西蒙购物网(XML配置方式)
  4. ASP運行Excel.Application出錯
  5. 404 NOT FOUND!
  6. 21世纪C语言(影印版)
  7. 苹果计算机打音乐,给苹果手机“隔空投送”更多的音乐和文件!
  8. SUBMAIL 短网址 API 授权与验证机制
  9. 鸿蒙系统桌面建文件夹,怎样把桌面上的文件放在一个文件夹里
  10. 一零四五、FAILED: SemanticException [Error 10293]: Unable to create temp file for insert values Expressio
  11. 数字IC入门基础(汇总篇)
  12. 静态模型,动态模型!
  13. 提莫攻击 findPoisonedDuration
  14. Unity 颜色板|调色板|无级变色功能
  15. 另一个伊甸 更新进度 23-01-27
  16. 【Week 15 作业A】ZJM 与霍格沃兹
  17. leafnotification_Notification Service
  18. 公式宝典之颜色混合模式公式
  19. java 文件保存_java中怎么实现保存文件到本地
  20. Linux 进程结束前善后处理

热门文章

  1. html语言中的链接方式,什么是超链接_在html中设置超链接的方法 - 驱动管家
  2. 根目录_ubuntu 18.04 server版根目录只有4G?
  3. php多维数组key交换,php 根据key计算多维数组的和功能实例
  4. mysql错误码 1045_MySql错误代码1045的解决方法
  5. 单片机ADC采样算法----一阶低通滤波
  6. Java中Lambda表达式
  7. Extjs 从grid中导出Excel表格。后台为C#(绝对好用)(按照自己的需求修改版本)...
  8. putty的窗口关闭相关的可能需要注意的细节
  9. 电池图标不见了怎么解决
  10. wordpress函数wp_http_validate_url畸形IP绕过验证SSRF漏洞