当前位置:网站首页>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
总结
判断链表是否有环使用快慢指针
边栏推荐
- DLA模型(分类模型+改进版分割模型) + 可变形卷积
- 不要做巨婴了
- Leetcode: String 04 (reverse the words in the string)
- Netease Yunxin officially joined the smart hospital branch of China Medical Equipment Association to accelerate the construction of smart hospitals across the country
- Godson China Science and technology innovation board is listed: the market value is 35.7 billion yuan, becoming the first share of domestic CPU
- 360手机助手首家接入APP签名服务系统 助力隐私安全分发
- [Bayesian classification 2] naive Bayesian classifier
- QT based "synthetic watermelon" game
- How to create an OData service with the graphical modeler on the sap BTP platform
- Is there any risk in opening a securities registration account? Is it safe?
猜你喜欢

Redis + guava local cache API combination, performance burst!

Student information management system based on SSH Framework

About appium trample pit: encountered internal error running command: error: cannot verify the signature of (solved)

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)

卷积神经网络(CNN)详解及TensorFlow2代码实现

YuMinHong: New Oriental does not have a reversal of falling and turning over, destroying and rising again

The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?

花店橱窗布置【动态规划】

基于Qt实现的“合成大西瓜”小游戏

Background search, how to find the website background
随机推荐
Leetcode question brushing: String 06 (implement strstr())
leetcode刷题:字符串02( 反转字符串II)
Is there any risk in registering and opening an account for stock speculation? Is it safe?
Matrix calculator design for beginners of linear algebra based on Qt development
Convolutional neural network (CNN) explanation and tensorflow2 code implementation
Redis + guava local cache API combination, performance burst!
The postgraduate entrance examination in these areas is crazy! Which area has the largest number of candidates?
Twenty five of offer - all paths with a certain value in the binary tree
Yonghui released the data of Lantern Festival: the sales of Tangyuan increased significantly, and several people's livelihood products increased by more than 150%
Installation avec homebrew dans un environnement Mac OS [email protected]
Chapter 2 construction of self defined corpus
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
网络连接断开请刷新重试
Is there any risk in opening a mobile stock registration account? Is it safe?
Module 5 operation
SAP commerce cloud project Spartacus getting started
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
Is it safe to open a stock account with the QR code given by the CICC securities manager? I want to open an account
Establish a connection with MySQL
[Bayesian classification 3] semi naive Bayesian classifier