一、题目

给定一个链表,判断链表中是否有环。

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

示例3:

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。

进阶:你能用 O(1)(即,常量)内存解决此问题吗?

二、题解

  • 解法1:哈希表

遍历所有节点并把每个节点的引用存储到哈希表中。如果当前节点的引用已经存在于哈希表中,说明该链表是环形链表,返回 true;反之,则遍历到尾,即该节点的下一个节点为 null,该链表不是环形链表,返回 false。

时间复杂度:O(n),空间复杂度:O(n)。

function hasCycle($head) {
$hashMap = [];
$curr = $head;
while ($curr != null && $curr->next != null) {
if (in_array($curr, $hashMap)) {
return true;
}
$hashMap[] = $curr;
$curr = $curr->next;
}
return false;
}
  • 解法2:双指针——快慢指针

快指针:一次两步

慢指针:一次一步

假设一个环形赛道,两个运动员 A 和 B,A 领先 B 一步:

在一个圆里,速度快的 A 在跑了 n 圈后,一定能遇到速度慢的 B

——对应链表,就是两个指针重合

如果不是圆,速度快的 A 一定先到达终点,则说明不存在环

——对应链表,就是结尾指针指向null

时间复杂度:O(n),空间复杂度:O(1)。

function hasCycle($head) {
if ($head == null || $head->next == null) {
return false;
}
//快慢指针
$slow = $head;
$fast = $head->next;
while ($slow != $fast) {
//没有环,fast走到链表尾部,fast为空或者fast的next为空
if ($fast == null || $fast->next == null) {
return false;
}
$slow = $slow->next;
$fast = $fast->next->next;
}
return true;
}

快慢指针法是看了题解后做出来的,一开始有点不明白:为什么快指针速度为 2,慢指针速度为 1?快指针速度不能为 3 甚至更大吗?

画图画了几遍,大概理解了:

fast 走 2 步,slow走 1 步,如果有环,则 fast 会碰到 slow 或者 fast 会走到 slow 的后面。

如果是 fast 走到 slow 后面,这时候两个指针每走一次它们之间的距离就会缩小 1,最后无论多远两个指针肯定会相遇。

但是如果是 fast 走 3 步,slow 走 1 步,同样是前面的情况,它们之间的距离每次缩小 2,最后 fast 又跑到 slow 前面去了,所以如果 fast 设置为 3,需要多加一个判断条件,见下面代码 while 处。

function hasCycle($head) {
if ($head == null || $head->next == null) {
return false;
}
//快慢指针
$slow = $head;
$fast = $head->next->next;
while ($slow != $fast || $slow->next != $fast) {
//没有环,fast走到链表尾部,fast为空或者fast的next为空
if ($fast == null || $fast->next == null) {
return false;
}
$slow = $slow->next;
$fast = $fast->next->next->next;
}
return true;
}

LeetCode#141-Linked List Cycle-环形链表的更多相关文章

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

  2. [LeetCode] 141. Linked List Cycle 单链表中的环

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

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

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

  5. 【LeetCode】Linked List Cycle(环形链表)

    这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...

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

  7. 141 Linked List Cycle 环形链表

    给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...

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

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

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

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

随机推荐

  1. 五分钟用Docker快速搭建Go开发环境

    挺早以前在我写过一篇用 `Docker`搭建LNMP开发环境的文章:[用Docker搭建Laravel开发环境](http://mp.weixin.qq.com/s?__biz=MzUzNTY5MzU ...

  2. 解决unrecognized import path "xxx"

    $ export GOPROXY=https://goproxy.io 环境变量配置上面这句即可 https://goproxy.io 是一个goproxy.io这个开源项目提供的公开代理服务. 使用 ...

  3. C#中无边框窗体拖动

    //定义一个bool变量标识是否拖动窗体 private bool isMove = false; //记录鼠标的位置 private Point point; 定义的变量 /// <summa ...

  4. Linux服务器(Centos)上安装jexus

    哈子是Jexus Jexus是一款Linux平台上的高性能WEB服务器和负载均衡网关,Jexus Web Service,简称JWS,以支持ASP.NET.ASP.NET CORE.PHP为特色, 同 ...

  5. ADO.NET 的使用(一)

    一.ADO.NET概要 ADO.NET 是一组向 .NET Framework 程序员公开数据访问服务的类. ADO.NET 为创建分布式数据共享应用程序提供了一组丰富的组件. 它提供了对关系数据.X ...

  6. ABP实践(4)-abp前端vue框架之简单商品增删改查(帮助刚入门的新手快速了解怎么才能加入自己的功能并运行起来)

    提示:如有不明白的地方请先查看前3篇ABP实践系列的文章 1,下载及启动abp项目前后端分离(netcore+vue) 2,修改abp数据库为mysql 3,商品系列api接口(本文主要依赖在这个商品 ...

  7. 批量redis未授权检测工具&批量redis弱口令爆破工具

    今天需要然后就百度搜索了一波,然后自己稍微改了一下: #!/usr/bin/python3 # -*- coding: utf-8 -*- """ @Author: 偷来 ...

  8. LeetCode45——从搜索算法推导到贪心

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode系列的第25篇文章,今天我们一起来看的是LeetCode的第45题,Jump Game II. 有同学后台留言问我说, ...

  9. [贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Fishing Master Time Limit: 2000/1000 MS (Java/Othe ...

  10. Java的集合框架综述

    集合 用于存储和管理数据的实体被称为数据结构(data structure).数据结构可用于实现具有不同特性的集合对象,这里所说的集合对象可以看作一类用于存储数据的特殊对象. 集合内部可以采用某种数据 ...