当前位置:网站首页>第一讲:链表中环的入口结点
第一讲:链表中环的入口结点
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;
}
};
边栏推荐
- 在网页中打开展示pdf文件
- [the most detailed in history] statistical description of overdue days in credit
- Usage of limit and offset (Reprint)
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- limit 与offset的用法(转载)
- STM32F1與STM32CubeIDE編程實例-旋轉編碼器驅動
- 35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
- ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
- 面试题详解:用Redis实现分布式锁的血泪史
- 51与蓝牙模块通讯,51驱动蓝牙APP点灯
猜你喜欢
测试流程不完善,又遇到不积极的开发怎么办?
[programming problem] [scratch Level 2] March 2019 draw a square spiral
腾讯安全发布《BOT管理白皮书》|解读BOT攻击,探索防护之道
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
Seven years' experience of a test engineer -- to you who walk alone all the way (don't give up)
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
【史上最详细】信贷中逾期天数统计说明
Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
随机推荐
每日刷题记录 (十六)
Cmake learning notes (1) compile single source programs with cmake
Open display PDF file in web page
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
什么是负载均衡?DNS如何实现负载均衡?
[C language] objective questions - knowledge points
Vscode software
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
3 years of experience, can't you get 20K for the interview and test post? Such a hole?
浪潮云溪分布式数据库 Tracing(二)—— 源码解析
[programming questions] [scratch Level 2] March 2019 garbage classification
Using Google test in QT
Su embedded training - day4
Kubectl's handy command line tool: Oh my Zsh tips and tricks
Binder核心API
ROS从入门到精通(九) 可视化仿真初体验之TurtleBot3
Trust orbtk development issues 2022
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
商品的设计等整个生命周期,都可以将其纳入到产业互联网的范畴内