当前位置:网站首页>链表——142. 环形链表 II
链表——142. 环形链表 II
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
- 环形链表 II
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
2 题目示例



3 题目提示
- 链表中节点的数目范围在范围 [0, 104] 内
- -105 <= Node.val <= 105
- pos 的值为 -1 或者链表中的一个有效索引
4 思路
方法一:哈希表
一个非常直观的思路是:我们遍历链表中的每个节点,并将它记录下来;一旦遇到了此前遍历过的节点,就可以判定链表中存在环。借助哈希表可以很方便地实现。
复杂度分析
时间复杂度:O(N)O(N),其中 NN 为链表中节点的数目。我们恰好需要访问链表中的每一个节点。
空间复杂度:O(N)O(N),其中 NN 为链表中节点的数目。我们需要将链表中的每个节点都保存在哈希表当中。
方法二:快慢指针
我们使用两个指针,fast 与 slow。它们起始都位于链表的头部。随后,slow 指针每次向后移动一个位置,而 fast 指针向后移动两个位置。如果链表中存在环,则 fast 指针最终将再次与 slow 指针在环中相遇。
如下图所示,设链表中环外部分的长度为 aa。 slow 指针进入环后,又走了 bb 的距离与 fast 相遇。此时, fast 指针已经走完了环的 nn 圈,因此它走过的总距离为 a+n(b+c)+b=a+(n+1)b+nc。
根据题意,任意时刻, fast 指针走过的距离都为 slow 指针的 22 倍。因此,我们有
a+(n+1)b+nc=2(a+b)*a=c+(n−1)(b+c)
有了 a=c+(n-1)(b+c) 的等量关系,我们会发现:从相遇点到入环点的距离加上 n−1 圈的环长,恰好等于从链表头部到入环点的距离。
因此,当发现 slow 与 fast 相遇时,我们再额外使用一个指针 ptr。起始,它指向链表头部;随后,它和 slow 每次向后移动一个位置。最终,它们会在入环点相遇。
复杂度分析
时间复杂度:O(N)O(N),其中 NN 为链表中节点的数目。在最初判断快慢指针是否相遇时,\textit{slow}slow 指针走过的距离不会超过链表的总长度;随后寻找入环点时,走过的距离也不会超过链表的总长度。因此,总的执行时间为 O(N)+O(N)=O(N)O(N)+O(N)=O(N)。
空间复杂度:O(1)O(1)。我们只使用了 \textit{slow}, \textit{fast}, \textit{ptr}slow,fast,ptr 三个指针。
5 我的答案
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode pos = head;
Set<ListNode> visited = new HashSet<ListNode>();
while (pos != null) {
if (visited.contains(pos)) {
return pos;
} else {
visited.add(pos);
}
pos = pos.next;
}
return null;
}
}
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head, fast = head;
while (fast != null) {
slow = slow.next;
if (fast.next != null) {
fast = fast.next.next;
} else {
return null;
}
if (fast == slow) {
ListNode ptr = head;
while (ptr != slow) {
ptr = ptr.next;
slow = slow.next;
}
return ptr;
}
}
return null;
}
}
边栏推荐
- MySQL paging
- MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive
- 只会“点点点”,凭什么让开发看得起你?
- 2022,软测人的平均薪资,看完我瞬间凉了...
- Nodejs ctf 基础
- Redis cluster setup
- 07【Path、Files类的使用】
- Best practice | using Tencent cloud AI character recognition to realize enterprise qualification certificate recognition
- 08 [AIO programming]
- Lanqiao cup provincial training camp - commonly used STL
猜你喜欢

HCIP OSPF接口网络类型实验 第四天

Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution

【C】 Understanding C language variable scope and life cycle from memory

Semaphore详解

聊聊软件测试-自动化测试框架

Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error

Talk about software testing - automated testing framework

Zynq TTC usage

Lanqiao cup provincial training camp - commonly used STL

Fifty lectures of Euler (I)
随机推荐
Xilinx FPGA Microblaze AXI_ IIC usage and experience
SSH跨平台终端工具tabby推荐
Fiddler packet capture tool summary
JMeter接口测试步骤-安装教程-脚本录制-并发测试
16 tips for system administrators to use iptables
HDU5667 Sequence
High frequency written test questions (Weilai)
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
Research on parameter setting of MATLAB FFT
只会“点点点”,凭什么让开发看得起你?
Ask n! How many zeros are there behind
[golang] golang implements the post request to send form type data function
网络爬虫之短信验证
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
Semaphore详解
[QNX hypervisor 2.2 user manual]9.2 CmdLine
Ctfshow ThinkPHP topic 1
In BS4.String and Difference between text
[golang] golang实现截取字符串函数SubStr