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. 从官方的BZR源安装avant-window-navigator

    资料来自: http://blog.163.com/azhai@126/blog/static/111056312008315842433/http://www.ibentu.org/2007/09/ ...

  2. poj 2195 Going Home(最小费最大流)

    poj 2195 Going Home Description On a grid map there are n little men and n houses. In each unit time ...

  3. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  4. PO*创建标准采购订单

    --   l_iface_rec       po_headers_interface%ROWTYPE; 校验头相关信息 ) INTO l_po_count FROM po_headers_all p ...

  5. C语言第一个例子hello world

    1.用文本编辑器编辑代码如下,然后保存为hello.c文件 #include <stdio.h> int main(void){ printf("hello world" ...

  6. Loadrunner脚本编程(2)-VuGen脚本文件的开发过程

    http://www.360doc.com/content/10/0806/13/1698198_44076570.shtml 1.定义测试项目的目标,环境,脚本,测试数据,硬件等.脚本应该符合编码规 ...

  7. Linux 调优方案, 修改最大连接数-ulimit

    Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限制.下面我把某linux用户的最 ...

  8. scala中:: , +:, :+, :::, +++的区别

    4种操作符的区别和联系 一. ::   该方法被称为cons,意为构造,向队列的头部追加数据,创造新的列表. 用法为 x::list,其中x为加入到 头部的元素,无论x是列表与否,它都只将成为新生成列 ...

  9. Drupal administration theme

    Drupal允许为管理后台设置独立的theme,保存在系统变量variable_get('admin_theme'). Drupal使用全局变量$theme来保存当前请求对应的主题.Drupal在启动 ...

  10. Ubuntu的一些小技巧, 备忘

    Ubuntu下打开Scroll Lock键盘灯 一直以为灯坏了, 后来发现在win7下工作正常... 原来是跟系统有关系的. 在Ubuntu18.04下可以通过这个命令开关Scroll Lock灯 # ...