当前位置:网站首页>Lecture 1: the entry node of the link in the linked list
Lecture 1: the entry node of the link in the linked list
2022-07-08 00:28:00 【Fight! Sao Nian!】
subject : The entry node of a link in a list
Given a linked list , If it contains rings , Then the inlet node of the output ring .
If it does not contain a ring , The output null.
Data range
node val Value range [1,1000].
Chain length [0,500].
Examples
Given the linked list shown above :
[1, 2, 3, 4, 5, 6]
2
Be careful , there 2 Indicates that the number is 2 The node of , Node number from 0 Start . So the number is 2 The node of is val be equal to 3 The node of .
Then the inlet node of the output ring 3.
title :
Method 1 : Hashtable
We can use unordered_set, To save the node pointer , If the same pointer is found (set The size of the remains the same ), It means the entry node
Method 2 : Speed pointer
principle : Two steps at a time , Slow pointer one step at a time , If there is a ring, it must meet
Pictured , Suppose the slow pointer starts from a Go to the b Finally, I met the fast pointer in c, If the slow pointer returns to b spot , Then the fast pointer will return to d spot ( Because I will walk more y Step )
Then we can find that when the slow pointer goes b spot , Then the pointer will go to d spot ( Because of the fallback ), that x+y It will be an integral multiple of the circle ( In fact, the fast pointer may have gone through many circles , But it doesn't affect , It is equivalent to the remainder operation )
Finally, we move the slow pointer to the starting point , The two pointers take the same number of steps , Will meet at the entrance ( Because the fast pointer has gone y Step , To walk again x Step will definitely be b spot , And slow the pointer x Step will also be in b spot )
Number of executions :x+y+2x+2y+2x=5x+3y( because x+y Less than the total length , So time complexity 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;
}
};
边栏推荐
- redis你到底懂不懂之list
- Basic principle and usage of dynamic library, -fpic option context
- 去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
- 玩转Sonar
- Sqlite数据库存储目录结构邻接表的实现2-目录树的构建
- How to add automatic sorting titles in typora software?
- Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
- [programming problem] [scratch Level 2] December 2019 flying birds
- [question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
- DNS 系列(一):为什么更新了 DNS 记录不生效?
猜你喜欢
搭建ADG过程中复制报错 RMAN-03009 ORA-03113
3年经验,面试测试岗20K都拿不到了吗?这么坑?
Kubernetes Static Pod (静态Pod)
paddle一个由三个卷积层组成的网络完成cifar10数据集的图像分类任务
Deep dive kotlin synergy (XXII): flow treatment
Reentrantlock fair lock source code Chapter 0
单机高并发模型设计
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
Service Mesh介绍,Istio概述
[研发人员必备]paddle 如何制作自己的数据集,并显示。
随机推荐
【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
[the most detailed in history] statistical description of overdue days in credit
动态库基本原理和使用方法,-fPIC 选项的来龙去脉
Solution to prompt configure: error: curses library not found when configuring and installing crosstool ng tool
单机高并发模型设计
Binder核心API
80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
LeetCode刷题
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
How to put recyclerview in nestedscrollview- How to put RecyclerView inside NestedScrollView?
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
new和delete的底层原理以及模板
[programming problem] [scratch Level 2] draw ten squares in December 2019
3年经验,面试测试岗20K都拿不到了吗?这么坑?
52岁的周鸿祎,还年轻吗?
Development of a horse tourism website (optimization of servlet)
QT establish signal slots between different classes and transfer parameters
5g NR system messages