LeetCode: Linked List Cycle 解题报告
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 解题报告的更多相关文章
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 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 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- [Done]mysql in (#{list}) 只能查询/删除第一条的问题
数据如下(注意age是int类型): sql如下(注意是#不是$): java代码: Mybatis日志(只返回一笔记录): 直接在mysql中执行(age是int类型,注意参数带引号,确认jdbc是 ...
- Python之str方法
# -*- coding: utf-8 -*- #python 27 #xiaodeng #Python之str方法 #http://python.jobbole.com/82655/ #str为一个 ...
- SXi5.5不识别硬件驱动的光盘定制
SXi5.5不识别硬件驱动的光盘定制~~RealTek8111E,Intel217V,Z97 AHCI [复制链接] gmx168 电梯直达 1# 发表于 2014-9-23 17:06 ...
- 通俗的理解HTTPS以及SSL中的证书验证
一.HTTPS的安全性体现在哪 HTTP(超文本传输协议,Hyper Text Transfer Protocol)是我们浏览网站信息传输最广泛的一种协议.HTTPS(Hyper Text Trans ...
- VS2010没有Intellisense(智能感知)的解决办法
VS2010没有Intellisense(智能感知)的解决办法 Visual Studio 2010 的Intellisense是依赖于Microsoft SQL Server Compact 3.5 ...
- Google Volley框架之https请求
先插一句.Google出的volley框架本身是支持https请求的,可是仅仅是针对有第三方机构认证过的. 假设自己随便在网上搞的一个证书,那volley是不支持请求的. 本文讲下怎样让volley支 ...
- StringBuilder.AppendFormat(String, Object, Object) 方法
将通过处理复合格式字符串(包含零个或零个以上格式项)返回的字符串追加到此实例. 每个格式项都替换为这两个参数中任意一个参数的字符串表示形式. 说明: public StringBuilder Appe ...
- configure: error: newly created file is older than distributed files!
在linux下安装软件包的时候,有时候提示 configure: error: newly created file is older than distributed files!Check you ...
- 1.Java基础-面向对象编程思想(封装继承多态接口)
封装: 1.定义:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. 2.封装的目的是:增强安全性和简化编程,使用者不必了解具体的实现细节,而只是要通过外部接口,一特定的 ...
- Unix环境高级编程(七)fork函数总结
在Unix/Linux中用fork函数创建一个新的进程.进程是由当前已有进程调用fork函数创建,分叉的进程叫子进程,创建者叫父进程.该函数的特点是调用一次,返回两次,一次是在父进程,一次是在子进程. ...