当前位置:网站首页>leetcode:141. 环形链表【哈希表 + 快慢指针】
leetcode:141. 环形链表【哈希表 + 快慢指针】
2022-06-26 21:38:00 【白速龙王的回眸】

分析
如果用哈希表看内存位置也是可以的
但是如果要更小的内存空间的话,可以使用快慢指针
floyd判圈:如果有环,快慢指针必定相遇
算法判断:
如果head或者head.next不存在就必定没有环
然后slow从head开始,fast从head.next开始
一直看看slow和fast相等与否,用while循环,一旦相等就肯定有环
在while循环内,如果没有fast或者没有fast.next就肯定没有环了
然后slow走一步,fast走两步
ac code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return False
# start
slow, fast = head, head.next
# if have cycle, slow and fast must meet
while slow != fast:
if not fast or not fast.next:
return False
slow = slow.next
fast = fast.next.next
return True
总结
判断链表是否有环使用快慢指针
边栏推荐
- 证券注册开户有没有什么风险?安全吗?
- The network connection is disconnected. Please refresh and try again
- Kdd2022 𞓜 unified session recommendation system based on knowledge enhancement prompt learning
- 卷积神经网络(CNN)详解及TensorFlow2代码实现
- How to install mysql8.0 database under Windows system? (Graphic tutorial)
- leetcode刷题:字符串06(实现 strStr())
- Web crawler 2: crawl the user ID and home page address of Netease cloud music reviews
- Matrix calculator design for beginners of linear algebra based on Qt development
- leetcode刷题:字符串03(剑指 Offer 05. 替换空格)
- The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
猜你喜欢

2022年,中轻度游戏出海路在何方?

Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%

VB.net类库——4给屏幕截图,裁剪

Configure redis master-slave and sentinel sentinel in the centos7 environment (solve the problem that the sentinel does not switch when the master hangs up in the ECS)
![[Bayesian classification 4] Bayesian network](/img/5b/348e00c920028e33ca457196586d36.png)
[Bayesian classification 4] Bayesian network

回首望月

Matrix calculator design for beginners of linear algebra based on Qt development

亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践

Looking back at the moon

基于Qt实现的“合成大西瓜”小游戏
随机推荐
茂莱光学科创板上市:拟募资4亿 范一与范浩兄弟为实控人
财务费用分析怎么分析
第2章 构建自定义语料库
Leetcode: hash table 08 (sum of four numbers)
不要做巨婴了
Leetcode question brushing: String 02 (reverse string II)
lotus configurations
Shiniman household sprint A shares: annual revenue of nearly 1.2 billion red star Macalline and incredibly home are shareholders
Arrête d'être un bébé géant.
线性模型LN、单神经网络SNN、深度神经网络DNN与CNN测试对比
[solution] sword finger offer 15 Number of 1 in binary (C language)
「连续学习Continual learning, CL」最新2022研究综述
Matrix derivation and its chain rule
网易云信正式加入中国医学装备协会智慧医院分会,为全国智慧医院建设加速...
Student information management system based on SSH Framework
y48.第三章 Kubernetes从入门到精通 -- Pod的状态和探针(二一)
Twenty five of offer - all paths with a certain value in the binary tree
Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
Listing of maolaiguang discipline on the Innovation Board: it is planned to raise 400million yuan. Fanyi and fanhao brothers are the actual controllers
Leetcode(452)——用最少数量的箭引爆气球