当前位置:网站首页>第一讲:链表中环的入口结点
第一讲:链表中环的入口结点
2022-07-07 22:21:00 【奋斗吧!骚年!】
题目:链表中环的入口结点
给定一个链表,若其中包含环,则输出环的入口节点。
若其中不包含环,则输出null。
数据范围
节点 val 值取值范围 [1,1000]。
链表长度 [0,500]。
样例
给定如上所示的链表:
[1, 2, 3, 4, 5, 6]
2
注意,这里的2表示编号是2的节点,节点编号从0开始。所以编号是2的节点就是val等于3的节点。
则输出环的入口节点3.
题目解析:
方法一:哈希表
我们可以使用unordered_set,来保存节点指针,如果发现有相同的指针(set的大小不变),则表示是入口节点
方法二:快慢指针
原理:快指针每次走两步,慢指针每次走一步,如果有环则一定会相遇
如图,假设慢指针从a走到b最后与快指针相遇于c,如果慢指针返回到b点,则快指针会返回到d点(因为会多走y步)
那么我们可以发现当慢指针走到b点,那么快指针就会走到d点(因为回退得出的),那么x+y就会是圈的整数倍(其实可能快指针已经在圈里走过很多圈了,但是这不影响,相当于取余操作)
最后我们将慢指针移到起点,两个指针走同样的步数,则就会在入口相遇(因为快指针已经走了y步,再走x步肯定会在b点,而慢指针走x步也会在b点)
执行次数:x+y+2x+2y+2x=5x+3y(因为x+y小于总长度,所以时间复杂度0(n))
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *entryNodeOfLoop(ListNode *head) {
if(!head)return NULL;
set<ListNode *> s;
while(head->next)
{
int size=s.size();
s.insert(head);
if(size==s.size())return head;
head=head->next;
}
return NULL;
}
};
边栏推荐
- Using Google test in QT
- Zhou Hongqi, 52 ans, est - il encore jeune?
- Reading notes 004: Wang Yangming's quotations
- Use filters to count URL request time
- LinkedBlockingQueue源码分析-新增和删除
- 如果在构造函数中抛出异常,最好的做法是防止内存泄漏?
- [研发人员必备]paddle 如何制作自己的数据集,并显示。
- Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
- Tools for debugging makefiles - tool for debugging makefiles
- A brief history of information by James Gleick
猜你喜欢

【编程题】【Scratch二级】2019.09 绘制雪花图案

爬虫实战(八):爬表情包

某马旅游网站开发(登录注册退出功能的实现)

The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log

Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe

Basic learning of SQL Server -- creating databases and tables with the mouse

DNS 系列(一):为什么更新了 DNS 记录不生效?

Reptile practice (VIII): reptile expression pack

3年经验,面试测试岗20K都拿不到了吗?这么坑?

Jouer sonar
随机推荐
Robomaster visual tutorial (11) summary
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
【史上最详细】信贷中逾期天数统计说明
limit 与offset的用法(转载)
Teach you to make a custom form label by hand
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
[programming problem] [scratch Level 2] March 2019 draw a square spiral
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
QT creator add JSON based Wizard
Basic principle and usage of dynamic library, -fpic option context
快速上手使用本地测试工具postman
[the most detailed in history] statistical description of overdue days in credit
单机高并发模型设计
Operating system principle --- summary of interview knowledge points
51与蓝牙模块通讯,51驱动蓝牙APP点灯
Open display PDF file in web page
Robomaster visual tutorial (10) target prediction
智慧监管入场,美团等互联网服务平台何去何从
Relevant methods of sorting arrays in JS (if you want to understand arrays, it's enough to read this article)
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3