141. Linked List Cycle【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) return false;
ListNode slow = head;
ListNode fast = head.next;
while(slow!=null && fast!= null && fast.next!=null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
}
return false;
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
}
141. Linked List Cycle【Easy】【判断链表是否存在环】的更多相关文章
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [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 & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 【easy】141. Linked List Cycle
非常简单的题:判断链表有没有环(用快慢指针) /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
随机推荐
- 【C++对象模型】第一章 关于对象
1.C/C++区别 C++较之C的最大区别,无疑在于面向对象,C程序中程序性地使用全局数据.而C++采用ADT(abstract data tpye)或class hierarchy的数据封装.类相较 ...
- redis linux下的环境搭建
系统 CentOS7 Redis 官网下载 https://redis.io/download 1.下载解压 [root@TestServer-DFJR programs]# /usr/loca ...
- iOS 网络请求--- 配置info.plist文件
一.配置info.plist <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitra ...
- rsync的命令参数【转】
本篇文章,我们只介绍rsync的命令参数. rsync参数的具体解释如下: -v, –verbose 详细模式输出 -q, –quiet 精简输出模式 -c, –checksum 打开校验开关,强制对 ...
- js获取摄像头视频流
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- leaflet一个前端gis框架,比openlayer更简单
leaflet一个前端gis框架,比openlayer更简单 作者github:https://github.com/mourner?tab=overview&from=2009-12-01& ...
- 「6月雅礼集训 2017 Day8」gcd
[题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ...
- 基本控件文档-UISlider属性---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...
- 【洛谷 P2762】 太空飞行计划问题(最大权闭合图)
题目链接 最大权闭合图模型,参考 具体做法是从源点向每个实验连一条流量为这个实验的报酬的边,从每个实验向这个实验需要的所有器材各连一条流量为\(INF\)的边,再从每个器材向汇点连一条流量为这个器材的 ...
- bzoj 1202 并查集
首先我们知道若干区间和信息,判断给出信息是否合法,可以用并查集维护,我们用dis[x]表示x到father[x]的距离为多少,即区间father[x]到x的长度,这样我们可以在路径压缩的时候维护dis ...