题目说明

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

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

思路

一道比较出名的面试题,思路是用一个快指针,一个慢指针。快指针每次向前进两步,慢指针每次向前进一步。如果链表有环的话,快指针因为没法到达链表尽头迟早会和慢指针相遇,因此如果在到达链表尽头前两者相遇就表示链表有环路。

代码

public class LinkedListCycle {

    public boolean hasCycle(ListNode head) {

           // IMPORTANT: Please reset any member data you declared, as

           // the same Solution instance will be reused for each test case.

       if(head==null)

         return false;

       ListNode slow=head;

        ListNode fast=head;

        while(fast!=null)

        {

          if(fast.next!=null)

          fast=fast.next.next;

          else  //如果快指针不能再往前跑了就说明链表没有环

             return false;

          slow=slow.next;

          if(slow==fast)

             return true;

        }

        return false;//如果到头了说明没有环

         }

}

[LeetCode]LinkedList Cycle的更多相关文章

  1. LeetCode & list cycle

    LeetCode & list cycle 链表是否存在环检测 singly-linked list 单链表 "use strict"; /** * * @author x ...

  2. 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍

    算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...

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

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

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

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

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  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. [LeetCode] 141&142 Linked List Cycle I & II

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

随机推荐

  1. hdu 1799 循环多少次?

    题目 题意:给出n,m,其中m表示有几层循环,求循环的次数 ①如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次OP运算: ②如果代码中出现 fori=1;i<=n ...

  2. float 为什么不能用== ,或者大于等于,或者小于等于

    本文尝试着将以下内容做一个浅显的解释,主要包括浮点数为什么是不精确的,浮点数为什么不能用==和!=直接比较,以及浮点数的比较方法等几个方面.如果那个地方说的不对还请各位看官不吝赐教!欢迎大家评论区讨论 ...

  3. Spring Boot 2 实践记录之 Powermock 和 SpringBootTest

    由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...

  4. Create Index语句的Include作用

    在 SQL Server 2005 中,可以通过将非键列添加到非聚集索引的叶级别来扩展非聚集索引的功能.通过包含非键列,可以创建覆盖更多查询的非聚集索引.这是因为非键列具有下列优点: 它们可以是不允许 ...

  5. C#在dataGridView中遍历,寻找相同的数据并定位

      1. C#在dataGridView中遍历,寻找相同的数据并定位   [c-sharp] view plain copy int row = dataGridView1.Rows.Count;// ...

  6. 安装docker ce版

    可参考 菜鸟教程:http://www.runoob.com/docker/centos-docker-install.html 官网教程:https://docs.docker.com/instal ...

  7. IdentityServer4中文文档

    欢迎IdentityServer4 IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架. 它在您的应用程序中启用以下功能: 认证即服务 ...

  8. Wpf 导出CSV文件

    /// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// <param name="dt" ...

  9. HEOI2014 南国满地堆轻絮

    题目链接:戳我 就是二分一个数,之后记录一个前缀max,然后和当前数做差再/2即可.(因为我们要使得原来的序列变成不下降序列,所以当然是要控制一个上限,以达到后面较小数能以尽可能小的代价增加) 代码如 ...

  10. LiLicense server OR Activation code

    JET BRAINS系列工具下载地址: https://www.jetbrains.com/products.html?fromMenu License server 输入下列两个任何一个点击 Act ...