当前位置:网站首页>leetcode:141. Circular linked list [hash table + speed pointer]
leetcode:141. Circular linked list [hash table + speed pointer]
2022-06-26 21:44:00 【Review of the white speed Dragon King】

analysis
It is also possible to use a hash table to see the memory location
But if you want smaller memory space , You can use the speed pointer
floyd Judge circle : If there are rings , The fast and slow pointers must meet
Algorithm judgment :
If head perhaps head.next If there is no ring, there must be no ring
then slow from head Start ,fast from head.next Start
Keep looking slow and fast Equal or not , use while loop , Once equal, there must be a ring
stay while Within the loop , without fast Or not fast.next There must be no ring
then slow Take a step ,fast Take two steps
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
summary
Judge whether the linked list has a ring and use the speed pointer
边栏推荐
- 股票炒股注册开户有没有什么风险?安全吗?
- Treasure and niche cover PBR multi-channel mapping material website sharing
- 亿级月活全民K歌Feed业务在腾讯云MongoDB中的应用及优化实践
- 聊聊我的远程工作体验 | 社区征文
- DLA模型(分类模型+改进版分割模型) + 可变形卷积
- Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
- Is there any risk in registering and opening an account for stock speculation? Is it safe?
- SAP Spartacus 中的依赖注入 Dependency Injection 介绍
- Final part of web crawler: send directional messages to 100000 Netease cloud users
- MacOS環境下使用HomeBrew安裝[email protected]
猜你喜欢

Introduction of classic wide & deep model and implementation of tensorflow 2 code

VB.net类库(进阶版——1)

Many gravel 3D material mapping materials can be obtained with one click

KDD2022 | 基于知识增强提示学习的统一会话推荐系统

The importance of using fonts correctly in DataWindow

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

Hands on deep learning pytorch version 3 - Data Preprocessing

【protobuf 】protobuf 昇級後帶來的一些坑

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

API管理之利剑 -- Eolink
随机推荐
不同的子序列问题I
Dynamic parameter association using postman
Introduction to dependency injection in SAP Spartacus
Leetcode question brushing: String 05 (Sword finger offer 58 - ii. left rotation string)
众多碎石3d材质贴图素材一键即可获取
Leetcode(763)——划分字母区间
DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
Sword finger offer 12 Path in matrix
Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
Is there any risk for flush to register and open an account? Is it safe?
中金证券经理给的开户二维码办理股票开户安全吗?我想开个户
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
网络爬虫2:抓取网易云音乐评论用户ID及主页地址
Application and Optimization Practice of 100 million level monthly live national karaoke feed service in Tencent cloud mongodb
[LeetCode]-链表-2
龙芯中科科创板上市:市值357亿 成国产CPU第一股
SAP Commerce Cloud 项目 Spartacus 入门
360 mobile assistant is the first to access the app signature service system to help distribute privacy and security
Matrix derivation and its chain rule