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?

这道题是快慢指针的经典应用。只需要设两个指针,一个每次走一步的慢指针和一个每次走两步的快指针,如果链表里有环的话,两个指针最终肯定会相遇。实在是太巧妙了,要是我肯定想不出来。代码如下:

C++ 解法:

class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
};

Java 解法:

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;
}
}

Github 同步地址:

https://github.com/grandyang/leetcode/issues/141

类似题目:

Linked List Cycle II

Happy Number

参考资料:

https://leetcode.com/problems/linked-list-cycle/

https://leetcode.com/problems/linked-list-cycle/discuss/44489/O(1)-Space-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 141. Linked List Cycle 单链表中的环的更多相关文章

  1. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  2. [LeetCode] 142. Linked List Cycle II 链表中的环 II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. [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 ...

  4. 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 ...

  5. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  6. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  7. [leetcode]141. Linked List Cycle判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  8. 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 ...

  9. LeetCode 141. Linked List Cycle环形链表 (C++)

    题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...

随机推荐

  1. ubuntu删除文件和文件夹的rm命令

    在Ubuntu中好多文件或文件夹是不能使用右键删除的,因此知道删除文件或文件夹的rm命令显得尤为重要. rm命令的语法 rm [选项] 文件名或文件夹名 rm命令的一些选项 -f.--force 强力 ...

  2. 使用NumPy、Numba的简单使用(二)

    本来要写NLP第三课动态规划的,日了,写到一半发现自己也不会了,理论很简单,动态规划咋回事也知道,但是实现在源码上还是有点难度,现在简单给予题目描述,小伙伴也可以来思考一下,例题一,我们现在有1元硬币 ...

  3. SqlServer 查看数据库、添加数据文件

    一.查看SqlServer实例的数据库列表 1).直接在SSMS(SqlServer Management Studio)管理工具里面 展开实例下面的所有数据库便可查看  2).使用Transact- ...

  4. C# - VS2019 WinFrm程序调用ZXing.NET实现条码、二维码和带有Logo的二维码的识别

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码的识别. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https: ...

  5. Laravel向表里插入字段

    执行: php artisan make:migration add_字段_to_表名_table --table=表名 此时在database/migrations文件夹下会生成一个相应文件,更改如 ...

  6. AOD.NET实现数据库事物Transaction

    在开始介绍文章主要内容前先简单说一下事务 1.事务介绍 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库 ...

  7. maven 学习---Maven添加远程仓库

    默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资源库 添加Java.ne ...

  8. centos7 安装gitlab及简单配置

    1.安装 wget -O gitlab.rpm https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-11.11.3 ...

  9. webpack开发指南1

    怎么安装Webpack 安装node.js 首先需要安装Node.js,node自带了包管理工具npm. 安装webpack 使用npm install webpack -g,webpack全局安装到 ...

  10. 使用IDEA创建Maven项目和Maven使用入门(配图详解)

    本文详解的讲解了使用IDEA创建Maven项目,及Maven的基础入门. 1.打开IDEA,右上角选择File->New->Project 2.如图中所示选择Maven(可按自己所需添加, ...