[LeetCode]LinkedList Cycle
题目说明
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的更多相关文章
- LeetCode & list cycle
LeetCode & list cycle 链表是否存在环检测 singly-linked list 单链表 "use strict"; /** * * @author x ...
- 别再埋头刷LeetCode之:北美算法面试的题目分类,按类型和规律刷题,事半功倍
算法面试过程中,题目类型多,数量大.大家都不可避免的会在LeetCode上进行训练.但问题是,题目杂,而且已经超过1300道题. 全部刷完且掌握,不是一件容易的事情.那我们应该怎么办呢?找规律,总结才 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- 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 ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 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 ...
- [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 ...
随机推荐
- spring aop方式配置事务中的三个概念 pointcut advice advisor
AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...
- Spark Structured Stream 2
❤Limitations of DStream API Batch Time Constraint application级别的设置. 不支持EventTime event time 比process ...
- ReactJS 官网案例分析
案例一.聊天室案例 /** * This file provided by Facebook is for non-commercial testing and evaluation * purpos ...
- Miniprofiler在swagger、vue、angular中的使用
本篇分为以下几个部分: 1.Swagger的简单应用 2.Miniprofier的后台配置 3.跨域配置 4.在angular中显示Miniprofier 5.在vue中显示Miniprofier ...
- C# Lock锁(个人随记)
先看看为什么要用锁 需求:多线程处理值的加减 static int NoLockData = 0; public static void NoLockNormalTest(int threadIn ...
- 解决win10不显示coreldraw x4/5/6菜单栏
1.下载插件 https://pan.baidu.com/s/1nQ1kgUNSD7-9sjUbv5M6XA 复制链接下载插件 2.导入插件到工作区 3.设置工具栏显示 4.把工具栏拖动到菜单栏位置, ...
- Java开发 小工具累计
array to list Integer[] spam = new Integer[] { 1, 2, 3 }; List<Integer> rlt = Arrays.asList(sp ...
- 浏览器对HTTP请求的编码行为
浏览器对请求的URL编码行为 浏览器会对请求的URL中非ASCII码字符进行编码.这里不是指对整个URL进行编码,而是仅仅对非ASCII码字符部分进行编码,详情请看下面的实验记录. 实验一:在URL参 ...
- pandas 对dataframe一列中某些值进行处理
https://github.com/Bifzivkar/Boutique-Travel-Services-Predict/blob/master/feature/5_extract_feature. ...
- 应该怎么理解 app = Flask(__name__)
初始化生成一个app对象,这个对象就是Flask的当前实例对象,后面的各个方法调用都是这个实例Flask会进行一系列自己的初始化,比如web API路径初始化,web资源加载,日志模块创建等.然后返回 ...