LeetCode#141-Linked List Cycle-环形链表
一、题目
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 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-环形链表的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】Linked List Cycle(环形链表)
这道题是LeetCode里的第141道题. 题目要求: 给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? 简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast, ...
- 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 ...
- 141 Linked List Cycle 环形链表
给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- [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 ...
- [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 ...
随机推荐
- LSP原则—关于正方形不是长方形
长方形有二个属性长和宽.并有一个设置长的方法和设置宽的方法,还有一个求面积的方法. 像下面 private int length; private int width; public void set ...
- 浅谈静态布局、流式布局,rem布局,弹性布局、响应式布局
静态布局: 特点:没有兼容性问题 PC:居中布局,所有样式使用绝对宽度/高度(px),设计一个Layout,在屏幕宽高有调整时,使用横向和竖向的滚动条来查阅被遮掩部分:移动设备:另外建立移动网站,单独 ...
- [日志分析]Graylog2进阶之获取Nginx来源IP的地理位置信息
如果你们觉得graylog只是负责日志收集的一个管理工具,那就too young too naive .日志收集只是graylog的最最基础的用法,graylog有很多实用的数据清洗和处理的进阶用法. ...
- java idea spring mvc 入门 最起码 我8080跑起来了
IDEA建立Spring MVC Hello World 详细入门教程 https://www.cnblogs.com/wormday/p/8435617.html
- 深入理解计算机系统 (CS:APP) 缓冲区漏洞实验 – Buffer Lab 解析
原文地址:https://billc.io/2019/05/csapp-cachelab/ 写在前面 这是 CSAPP 官网上的第 4 个实验 buflab,也是学校要求的第三个实验.这个实验比上一个 ...
- window 查看端口 杀端口
最近写项目,总是出现端口被占用的问题,原来傻傻的把电脑重启一下,终于有一天受不了了,想要想办法解决.刚开始从网上找了好多教程,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程( ...
- java web 获取 网页访问次数
ServletContext context = request.getServletContext(); /** * 从ServletContext中获取计数器对象 */Integer count ...
- Topshelf+Quartz3.0基于控制台应用程序快速开发可调度windows服务
1.TopShelf TopShelf是一个开源的跨平台的宿主服务框架.可通过.Net Core/.Net Framwork控制台应用程序快速开发windows服务,更加便于服务调试. 本文基于.Ne ...
- Django实现简单的用户添加、删除、修改等功能
一. Django必要的知识点补充 1. templates和static文件夹及其配置 1.1 templates文件夹 所有的HTML文件默认都放在templates文件夹下. 1.2 stati ...
- PHP中$$的应用
PHP中$表示一个变量的声明,$value='test':表示变量value的值是test. 而$$则好像是C语言中的指针,它指向一个变量值的一个变量. 例如:$$value='a';这句话的意思就是 ...