当前位置:网站首页>LeetCode 0142.环形链表 II
LeetCode 0142.环形链表 II
2022-07-28 13:13:00 【Tisfy】
【LetMeFly】142.环形链表 II
力扣题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:

输入:head = [3,2,0,-4], pos = 1 输出:返回索引为 1 的链表节点 解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:

输入:head = [1,2], pos = 0 输出:返回索引为 0 的链表节点 解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:

输入:head = [1], pos = -1 输出:返回 null 解释:链表中没有环。
提示:
- 链表中节点的数目范围在范围
[0, 104]内 -105 <= Node.val <= 105pos的值为-1或者链表中的一个有效索引
进阶:你是否可以使用 O(1) 空间解决此题?
方法一:哈希表
这道题类似 LeetCode 141.环形链表 ,可参考题解https://leetcode.letmefly.xyz/2022/07/27/LeetCode 0141.环形链表/的方法一
同样地,我们用哈希表记录每个节点是否出现过,之后遍历链表。如果遇到了出现过的节点,那么就说明这个节点是环的开始,直接返回这个节点即可。
如果遍历到了链表的末尾,就说明无环,返回nullptr
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是链表中的节点个数
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_set<ListNode*> se;
while (head) {
if (se.count(head))
return head;
se.insert(head);
head = head->next;
}
return nullptr;
}
};
方法二:快慢指针
这次“快慢指针”是数学方法,真的是挺玄学的。
具体我就不推公式了,感兴趣了可参考下官方博客的方法二。
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是链表中的节点个数
- 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
C++
来自官方题解:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast != nullptr) {
slow = slow->next;
if (fast->next == nullptr) {
return nullptr;
}
fast = fast->next->next;
if (fast == slow) {
ListNode *ptr = head;
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return nullptr;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126030761
边栏推荐
- 【LVGL事件(Events)】事件代码
- P1797 heavy transportation problem solution
- Qt5 development from introduction to mastery -- the first overview
- Postgresql14安装及主从配置
- 第六章 支持向量机
- Slam thesis collection
- Deploy application delivery services in kubernetes (Part 1)
- Graph traversal (BFS & DFS basis)
- Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
- DXF reading and writing: Chinese description of dimension style group codes
猜你喜欢

多线程与高并发(三)—— 源码解析 AQS 原理
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)

How to play a data mining game entry Edition

Socket class understanding and learning about TCP character stream programming

Operator3 - design an operator

算法---不同路径(Kotlin)

Qt5 development from introduction to mastery -- the first overview

SLAM论文合集
随机推荐
83.(cesium之家)cesium示例如何运行
Dojp1520 gate jumping problem solution
【LVGL事件(Events)】事件代码
什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
Poj3268 shortest path solution
牛客多校-Link with Level Edito I-(线性dp)
jenkins
解决uniapp微信小程序canvas不能引入字体的问题
30 day question brushing training (I)
产品经理:岗位职责表
7.依赖注入
浅谈WebSocket
掌握常见的几种排序-选择排序
【翻译】如何为你的私有云选择一个网络网关
了解BFC特性,轻松实现自适应布局
论文研读--Masked Generative Distillation
SLAM论文合集
Tutorial on the principle and application of database system (061) -- MySQL exercise: operation questions 21-31 (V)
线程阻塞的三种情况。
正则表达式