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 ...
随机推荐
- css实现图片在div中居中的效果
利用图片的margin属性将图片水平居中,利用div的padding属性将图片垂直居中. 结构代码同上: css代码如下: div {width:300px; height:150px; paddin ...
- CSS中Position几种属性的总结
定位position 定位方向:left right top bottom 静态定位:static 默认值,就是文档流 绝对定位:absolute 特点: 1.不占据原来的位置(脱标) ...
- pycharm 安装步骤
1.双击安装包 2.点击next 3.选择安装目录后点击next进入下一步 4.根据你电脑的实际情况选择安装32位还是64位 5.勾选 .py 后即可点击next进入下一步 6.默认,直接next 7 ...
- oracle建表和sqlserver建表
oracle declare num number;begin select count(1) into num from user_all_tables where Upper(Table_Name ...
- linux清除日志和文件缓存
1.查找大文件,从根目录 find / -type f -size +800M 2.查看磁盘挂载情况及挂载目录 df -lh 3.查看内存使用情况 free -m 4.清除缓存 echo 3 > ...
- mac下eclipse关联svn插件
由于新冠状病毒的疫情这一周都需要在家办公了,家里只有一个mac之前只是娱乐工具,今天不得不用它撸代码,无奈重新安装各种环境,mac和windows的环境安装区别还是很大的,今天差点折磨死我,尤其是在e ...
- Vivado工程常见报错及解决办法
1. 在进行自定义 IP 后,将自定义 IP 添加到当前的工程时,出现如下报错: [IP_Flow 19-167] Failed to deliver one or more file(s). [IP ...
- java 将Excel数据读取到ListMap
将Excel数据读取到ListMap /** * 将Excel数据读取到ListMap * @param file */ public static List<Map<String, Ob ...
- 无显示器无键盘的树莓派搭建NAS(samba)
使用软件Rufus烧录系统2020-02-13-raspbian-buster.img到TF卡后,在TF卡的文件夹内创建空文件ssh,再创建一个名为wpa_supplicant.conf的文件,内容为 ...
- Pytorch 感知机
单层感知机 \[\begin{aligned} & y = XW + b \\ & y = \sum x_i*w_i+b\\ \end{aligned} \] Derivative \ ...