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. windows 安装配置mysql 8,以及远程连接访问

    官网下载mysql8,https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip 1.解压zip包到安装目录 比如我的安 ...

  2. 网络编程之 requests 模块

    1. get 请求 1 import requests 2 url = 'http://api.xxxx.cn/api/user/stu_info?stu_name=xiaohei' 3 data = ...

  3. 数据库负载均衡 happroxy 中间器(Nginx)容器的安装与配置

    docker  镜像中安装haproxy 1.下载并安装haproxy镜像 docker pull happroxy # docker pull haproxy:1.7 2.查看镜像 docker i ...

  4. 第08组Beta冲刺(3/4)

    队名 八组评分了吗 组长博客链接(2分) 组员1李昕晖(组长) 过去两天完成了哪些任务 文字/口头描述 了解各个小组的进度与难以攻破的地方,晚上安排开会,安排新的冲刺任务. 重新分配小组及个人任务. ...

  5. bzoj 3924

    动态点分治好题 首先我们考虑一个暴力做法: 每次修改之后选一个点作为根搜索整棵树,然后换根dp即可 考虑每次换根时,移向的点的消耗会减少子树代价之和*边权,而其余部分代价会增加剩余代价*边权 这样每次 ...

  6. github秒变vscode

    方法一:github.sth -> github1s.sth 方法二:github保持登录状态,然后是英文输入法,点击'.'就行

  7. 使用SharpCompress压缩文件后把压缩的文件流传给前端

    1 SharpCompress版本 0.30.1 2 应用场景:前端传递某个标识符,如Id,查询和该Id相关联的文件,并把文件压缩,最后返回给前端.适用于压缩多个体积较小的文件,如果文件体系过大,可能 ...

  8. gpio 理解

    NVIC :NVIC_Init(&NVIC_Initsture); 1.NVIC只是设置某一种中断的优先级,而不是打开某种中断. 2.ppp_ITConfig():才是开/关具体某种中断使能位 ...

  9. java 操作 zookeeper

    pom.xml<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</a ...

  10. sqlserver substring 函数截取text格式文本格式乱码导致的定位错误的问题

    描述:使用 charindex 函数对 text 字段所要截取的内容下标读取例如:str(表字段名称-类型text)= <p>●123456</p> 截取 123 , inde ...