Linked List Cycle

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

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

SOLUTION 1:

经典快慢指针问题。如果存在环,fast, slow必然会相遇。就像2个速度不一样的人在环形跑道赛跑,总有一个时间他们会相遇。

如果fast到达了null,就是没有环,可以返回false.

 package Algorithms.list;

 import Algorithms.algorithm.others.ListNode;

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

LeetCode: Linked List Cycle 解题报告的更多相关文章

  1. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. 通过修改css文件来观察openerp表单中的col和colspan

    适用版本 openerp 6.1.1 问题的提出 在openerp的表单定义中, 要使用 colspan和col 指你定各个元素的占位, 前者说明了本元素占据其所在容器的列数, 后者说明了本元素作为容 ...

  2. oracle 11g完美卸载

      oracle 11g完美卸载 CreateTime--2018年4月22日17:07:19 Author:Marydon 对于oracle数据库的卸载,一定要卸载干净,否则,再次想装oracle时 ...

  3. spring注解配置quartz

    常规配置quartz可以参考我的另外一篇博文:http://www.cnblogs.com/yangzhilong/p/3349116.html spring配置文件里增加: 命令空间: http:/ ...

  4. CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端

    CAS单点登录系列: CAS 5.1.x 的搭建和使用(一)—— 通过Overlay搭建服务端 CAS5.1.x 的搭建和使用(二)—— 通过Overlay搭建服务端-其它配置说明 CAS5.1.x ...

  5. zabbix监控redis连接情况

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对red ...

  6. js访问url和cookie

    function QueryString() { var data = []; this.Read = function() { var aPairs, aTmp; var queryString = ...

  7. 使用自定义模板来弥补eclipse没有新建Filter的功能

    http://yufei.iteye.com/blog/125012 eclipse插件,并没有为我们提供Filter的新建功能,为此我们不得不每次都去新建个类,然后输入那繁琐的重复代码,这完全就是浪 ...

  8. python学习笔记013——模块

    1 模块module 1.1 模块是什么 模块是包含一系列的变量,函数,类等程序组 模块通常是一个文件,以.py结尾 1.2 模块的作用 1. 让一些相关的函数,变量,类等有逻辑的组织在一起,使逻辑更 ...

  9. go 学习笔记(4) ---项目结构

    go install和go build之争.目前,IDEA插件和LiteIDE都采用了go build.Eclipse插件采用了go install.官方推荐go install方式编译项目,官方项目 ...

  10. 两种方式— 在hive SQL中传入参数

    第一种: sql = sql.format(dt=dt) 第二种: item_third_cate_cd_list = " 发发发 " ...... ""&qu ...