题目说明

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. 团队博客-第三周:需求改进&系统设计(科利尔拉弗队)

    针对课堂讨论环节老师和其他组的问题及建议,对修改选题及需求进行修改 需求规格说明书: 1.打开网页,弹出询问时候创建账号.是:分配数字组成账号,用户填写密码,确定登录进入首页:否,用已有账号登录(传参 ...

  2. EBS 11i升级R12

     http://blog.csdn.net/y657356105/article/details/8181081 概述 从EBS 11i升级至R12,总的来说更变较大的就是多OU访问(MOAC)和表视 ...

  3. STS中配置MyBatis代码生成器

    1.STS工具菜单项Help > Eclipse Marketplace... 2.输入“mybatis”关键字搜索 3.选择MyBatis Generator 1.3.7进行安装 4.安装成功 ...

  4. linux系统编程之信号(四):alarm和可重入函数

    一,alarm() 在将可重入函数之前我们先来了解下alarm()函数使用: #include <unistd.h> unsigned int alarm(unsigned int sec ...

  5. JQuery fullcalender文档

    转载: http://blog.csdn.net/lgg2011. 使用方式, 引入相关js, css后, $(‘#div_name’).fullCalendar({//options});  接受的 ...

  6. 数据帮助类(DataHelper)

    /// <summary> /// 是否为空... /// </summary> /// <param name="str">数据值</p ...

  7. html隐藏元素

    <body> <div>display:元素的位置不被占用</div> <div id="div1" style="displa ...

  8. JavaScript原型与继承的秘密

    在GitHub上看到的关于JavaScript原型与继承的讲解,感觉很有用,为方便以后阅读,copy到自己的随笔中. 原文地址:https://github.com/dreamapplehappy/b ...

  9. Kafka send问题

    kafka 在send之后不会立即把消息发送到broker.会把消息发到producer所在电脑内存里,后端的IOThread会扫描内存,并从中取出消息进行消费. 在调用close()或者flush( ...

  10. 『原创』手把手教你搭建一个实用的油耗App(一)

    前言: 入行快10年,有点积蓄,三年前买了代步车.于是乎,汽车油耗开销就成了每个月都必须关注的问题.三年来,用过了无数油耗记录软件,比如最知名的“小熊油耗”,从第一次用,一直到最新一版,感觉越来越“臃 ...