141. Linked List Cycle (Easy)
ps:能力有限,若有错误及纰漏欢迎指正、交流
- Linked List Cycle (Easy)
https://leetcode.cn/problems/linked-list-cycle/description/
给定一个链表,判断该链表是否存在环
- 方法一:暴力解法
将之前访问过的元素全部储存,与现在访问元素的next进行对比,有相同便存在环
时间复杂度:O(n^2) 此处采用 静态存储需申请大于10^4的内存,而使用 静态内存则有所优化,但每增加一个元素就得申请一个元素的地方
空间复杂度:O(n)
- 方法二:快慢指针
思路:若快指针 **追上 **慢指针则 存在环
bool hasCycle(struct ListNode *head) {
struct ListNode *slowP=head;
struct ListNode *quickP=head;
/*疑问:假设存在环,会不会出现快指针追不上慢指针的情况(因为环上元素的个数及快慢指针速度的问题)*/
do{
if(slowP==NULL||quickP==NULL||quickP->next==NULL){
return false;
}
slowP=slowP->next;
quickP=quickP->next->next;
}while(slowP!=quickP);/*相遇*/
return true;
}
时间复杂度:O(n)
当链表中存在环时,每一轮移动后,快慢指针的距离将减小一。而初始距离为环的长度,因此至多移动 N 轮。
另外的 角度:如果 加快(即增大快指针的增量)快指针,则可以更快的判断 不存在环路。那么,出现两个疑问:
- 1.会不会出现无法相遇的情况?
- 2.放慢快指针,一定会加快 存在环路的判断速度吗?
空间复杂度:O(1),仅仅使用了两个指针
另外
这里请注意:
如果使用 while则需要将
struct ListNode *slowP=head;
struct ListNode *quickP=head;
修改为:
struct ListNode *slowP=head;
struct ListNode *quickP=head->next;
否则,无法进入while循环
do...while与 while类似,但do...while会确保至少执行一次循环。
141. Linked List Cycle (Easy)的更多相关文章
- 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 ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 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 ...
- 【算法分析】如何理解快慢指针?判断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 - LeetCode
Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...
- [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 (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [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【Easy】【判断链表是否存在环】
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
随机推荐
- git reset 加不加 --hard的区别
通常我们提交代码一般都是 git add ,git commit -m, git push的这么个流程.添加到暂存区,提交到git库生成版本号,push到远程仓库以供他人可以使用.这是一个完整的且 ...
- Cadvisor+prometheus+grafana
部署Cadvisor [root@localhost ~]# docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:ro ...
- docker搭建phpswoole实现http服务
一.创建Dockerfile FROM phpswoole/swoole # COPY ./www/ /var/www/ 二.同级目录下创建docker-compose.yml services: p ...
- IDEA导出带源码的war包
做作业时实验要求导出带源码的war包,网上找了一圈没找着,遂自行探索,摸出了些门道,在此分享. File->Project Structure->Project Setting->A ...
- shell_Day02
虽然差了不少天,但的确是第......一天 history 查看历史命令记录 !命令序号 查看命令并执行 -c 清空 关于命令历史的文件 关于命令历史的变量(环境变量) 命令补全 tab:制表符 \t ...
- Spring学习记事本
原因:原因:Application的启动类不能放在默认的java目录,必须放在建有包的目录下.
- Templates && Algorithms
-[]基础算法 -[]递推和递归 -[]排序算法 -[]高精度,压位 -[]分治 -[]二分 -[]三分 -[]搜索算法 -[]简单搜索 -[]哈希和状态保存 -[]双向bfs -[]启发式搜索和DS ...
- Linux 使用vsftpd服务传输文件
文件传输协议 FTP是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20.21号端口,其中端口20(数据端口)用于进行数据传输,端口21(命令端口)用于接受客户端发出的相关FTP命 ...
- ESXI不重启增加硬盘空间更新
ESXI虚拟机Linux添加新磁盘后,可以通过重新扫描SCSI总线,在不重启虚拟机的情况下添加SCSI设备在线扩容磁盘(不停机)后,添加磁盘无法识别的问题,尝试了多种办法,最终通过重新扫描SCSI设备 ...
- iOS学习十二之选择器控件UIPickerView
UIPickerView是一个简易的列表控件,用于提供有限个数的选项供用户选择. 它是通过代理和数据源的方法对其进行设置和数据源填充的,这种控件的设计模式也是代理模式的应用之一. 添加下面的代码即可实 ...