ps:能力有限,若有错误及纰漏欢迎指正、交流

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

另外的 角度:如果 加快(即增大快指针的增量)快指针,则可以更快的判断 不存在环路。那么,出现两个疑问:

  • 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)的更多相关文章

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

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

  3. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

  5. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  6. 141. Linked List Cycle - LeetCode

    Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...

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

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

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

随机推荐

  1. tomcat各个版本下载

    官网地址:https://archive.apache.org/dist/tomcat/

  2. 如何在 Linux 上扫描/检测新的 LUN 和 SCSI 磁盘

    当 Linux 系统连接到 SAN(存储区域网络)后,你需要重新扫描 iSCSI 服务以发现新的 LUN. 要做到这一点,你必须向存储团队提供 Linux 主机的 WWN 号和所需的 LUN 大小. ...

  3. jenkins +docker+python接口自动化之jenkins拉取gitee上的代码(四)

    1.背景 1.经过我们前面几轮的安装测试,我们已经安装了jenkins容器,python3,以及运行我们python代码所需要的第三方库在requirements.txt文件下统一安装. 2.需求 我 ...

  4. 删除没有刀路的刀具.txt

      1 UF_initialize(); 2 std::vector<tag_t>tool_tag; 3 std::vector<tag_t>del_tag; 4 tag_t ...

  5. Python编码转换图

  6. node.js发送短信验证码(附带60秒倒计时插件)

    推荐一个简单且功能齐全的发送短信验证码接口1.安装下载后的SDK只包含一个zhenzisms.js文件,直接导入到工程中即可使用.下载 2.用法引入模块 const zhenzismsClient = ...

  7. flutter RaisedButton 设置最小宽度和高度

    flutter中可以通过ButtonTheme为RaisedButton设置最小宽度,示例代码如下: ButtonTheme( minWidth: 200.0,//设置最小宽度 height: 100 ...

  8. 20211306 实验一《Python程序设计》实验报告

    202111306 丁文博 第一次实验报告 课程:<Python程序设计> 班级: 2113 姓名: 丁文博 学号:20211306 实验教师:王志强 实验日期:2022年3月24日 必修 ...

  9. DorisSQL与MySQL函数对照 差异篇

    ## 1.日期函数### 时区.```mysql -> convert_tz(dt,from_tz,to_tz)doris -> CONVERT_TZ(DATETIME dt, VARCH ...

  10. Mac截网页长屏的方法-谷歌浏览器

    打开chrome,左上角帮助,输入[开发者工具],回车,右边显示出一些html代码, 然后shift+command+p: 输入full,选择第一个capture full size screensh ...