题目说明

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

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

思路

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

代码

  1. public class LinkedListCycle {
  2.  
  3. public boolean hasCycle(ListNode head) {
  4.  
  5. // IMPORTANT: Please reset any member data you declared, as
  6.  
  7. // the same Solution instance will be reused for each test case.
  8.  
  9. if(head==null)
  10.  
  11. return false;
  12.  
  13. ListNode slow=head;
  14.  
  15. ListNode fast=head;
  16.  
  17. while(fast!=null)
  18.  
  19. {
  20.  
  21. if(fast.next!=null)
  22.  
  23. fast=fast.next.next;
  24.  
  25. else //如果快指针不能再往前跑了就说明链表没有环
  26.  
  27. return false;
  28.  
  29. slow=slow.next;
  30.  
  31. if(slow==fast)
  32.  
  33. return true;
  34.  
  35. }
  36.  
  37. return false;//如果到头了说明没有环
  38.  
  39. }
  40.  
  41. }

[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 5015 233矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 需要构造一个 n+2 维的矩阵. 就是要增加一维去维护2333这样的序列. 可以发现 2333 = 233 ...

  2. 个人项目:实现wc.exe(Java)

    本项目Github地址:https://github.com/NNewBoy/wc 项目相关要求 基本功能:(已实现) -c 统计文件字符数 -w 统计文件词的数目 -l 统计文件行数 扩展功能:(已 ...

  3. Create a site by Google Site - All Free

    Follow this link :  https://www.google.com/sites/help/intl/en/overview.html

  4. 实现EventHandler的监测

    的监测", "category":"", "tags":"", "publish":&qu ...

  5. 设计模式之命令模式(Command Pattern)

    一.什么是命令模式? 命令模式,封装了方法调用细节,以解耦请求者与执行者,具体流程如下: 1.从请求者(客户)的角度看 请求者(客户)发出请求 -> 调用者(系统)构造命令对象封装请求 -> ...

  6. SqlServer Session共享注意点

    公司下派任务,之前的网站是一台服务器,由于用户过多,负载过大,现在老大要求多加一台服务器.加就加贝,应该跟我这DEV没有 关系吧,应该不会碰到Source的吧.但是,之前网站有一些数据是放在Sessi ...

  7. asp.net 进行发送邮箱验证

    利用发送邮件验证码进行注册验证 需要引用using System.Net.Mail;命名空间 #region /// <summary> /// 发送邮件 /// </summary ...

  8. Restframework 视图组件与序列号组件的应用.

    models from django.db import models # Create your models here. class Course(models.Model): title=mod ...

  9. Java基础学习篇---------多线程

    一.编写两种多线程的方法 (1).Thread(它是继承Runnable的子类) class MyThread extends Thread{ private int ticket = 5; @Ove ...

  10. hdoj1045 Fire Net(二分图最大匹配)

    题意:给出一个图,其中有 . 和 X 两种,. 为通路,X表示墙,在其中放炸弹,然后炸弹不能穿过墙,问你最多在图中可以放多少个炸弹? 这个题建图有点复杂orz. 建图,首先把每一行中的可以放一个炸弹的 ...