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. js正则 -180 到180 小数点后无限位、el-input

    正则 -180 到180 小数点后无限位/^0$|^-?0\.\d*[1-9]$|^-?[1-9](\.\d*[1-9])?$|^-?[1-9]\d(\.\d*[1-9])?$|^-?1[0-7]\d ...

  2. MAMP redis.conf 位置 , nginx.conf位置

    /Applications/MAMP PRO.app/Contents/Resources/redis.conf /Applications/MAMP/conf/nginx/nginx.conf /A ...

  3. ADOConnection调用连接窗口

    uses   AdoConEd; 使用函数 1.EditConnectionString(ADOConnection); 2.PromptDataSource

  4. js 获取指定天数前日期

    getBeforeDate(n) {//传入天数 var n = n; var d = new Date(); var year = d.getFullYear(); var mon = d.getM ...

  5. Linux-非Root用户-配置深度学习环境

    1|01 简介 Anaconda:是一个开源的Python发行版本,其包含了conda.Python等软件包,numpy,pandas,scipy等科学计算包. Conda:包管理器, venv+pi ...

  6. 2022 ICPC沈阳合肥游记

    选赛区的时候很争议,除了沈阳是确定要选,队友对于合肥新赛区的看法很质疑,但我想选合肥,一是觉得人少,二是觉得强队会少,因为隔壁CCPC.然后就选了合肥,看情况选合肥确实很对. 一开始也不认为会拿牌,后 ...

  7. springboot[2.3.4.RELEASE]application.properties常用配置

    常用配置 spring.profiles.active=@spring.profiles.active@ pom文件中定义的配置文件 server.port=8080 web默认访问端口 server ...

  8. vue中使用Echart将一组数据展示出三种统计图

    1 <template> 2 <div class="container"> 3 <div id="myEchart" style ...

  9. Springboot项目记录1配置环境

    一.电脑商城项目: 项目功能:登录,注册,热销商品,用户管理(密码,个人信息,头像,收货地址).购物车(展示.增加.删除).订单模块. 二.开发顺序: 注册.登录.用户管理.购物车.商品.订单模块 三 ...

  10. 2022-03-02英语精读(Returning Youths)

    今天早上没写代码,记录一下英语学习吧~ flee to sw/ from sb(sth)----coastal city-----get one's break----clinch a job/ de ...