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

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

题意:

判断一个链表是否有环。

解决方案:

双指针,快指针每次走两步,慢指针每次走一步,

如果有环,快指针和慢指针会在环内相遇,fast == slow,这时候返回true。

如果没有环,返回false.

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {ListNode fast = head, slow = head;if(head == null || head.next == null) return false;while(fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;if(fast == slow){return true;}}return false;}
}

转载于:https://www.cnblogs.com/iwangzheng/p/5695174.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 单链表判圆算法

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

  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. 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. link 和@import 的区别?
  2. 一般物流网站建设有哪些必备版块?
  3. Linux之解析鼠标input事件数据
  4. 浅析HTML、CSS、JavaScript之间的联系与区别
  5. CentOS6.7安装SBT
  6. expect脚本的简单应用
  7. java 抽象类语法_JAVA基础语法8--多态/抽象类/抽象方法
  8. 【零基础学Java】—继承的概述(十九)
  9. LeetCode 724. Find Pivot Index
  10. Eclipse 工程配置与目录结构及各种文件夹(常用插件)
  11. [翻译]WPF控件库 MaterialDesignInXamlToolkit (2) Brush Names
  12. java 爬虫 html页面 parse,Java 爬虫 爬取html网页解析
  13. 写出线程同步相关的方法,以银行账号存储款为例
  14. 网易博客 android,android编译环境
  15. 可能是最简单暴力的卸载工具Geek Uninstaller
  16. 【Ps问题】PS旋转功能会让图片乱飞的解决方法
  17. 李嘉诚80个人生经典语录
  18. Linux好用命令之base64命令
  19. 沟通的艺术与处世智慧 ——戴尔卡耐基(笔记)
  20. yarn install出现异常 error An unexpected error occurred: “EIO: i/o error

热门文章

  1. 将外部准备好的sqlite导入到项目当中
  2. Java 连接MS Access数据库
  3. sphinx4 FrontEnd流程分析
  4. java 文件流的处理 文件打包成zip
  5. bzoj2034: [2009国家集训队]最大收益
  6. wait和notify的理解与使用
  7. 【线性代数】向量空间
  8. 模拟器genymotion的安装与配置
  9. hdu 2602 Bone Collector(01背包)
  10. 关于WS_CLIPCHILDREN和WS_CLIPSIBLINGS的理解