当前位置:网站首页>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
- Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
- RPA云电脑,让RPA开箱即用算力无限?
- Jouer sonar
- Summary of weidongshan phase II course content
- Two small problems in creating user registration interface
- RPA cloud computer, let RPA out of the box with unlimited computing power?
- Handwriting a simulated reentrantlock
- [question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
- fabulous! How does idea open multiple projects in a single window?
猜你喜欢
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game
The underlying principles and templates of new and delete
去了字节跳动,才知道年薪 40w 的测试工程师有这么多?
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
【笔记】常见组合滤波电路
[programming problem] [scratch Level 2] December 2019 flying birds
QT establish signal slots between different classes and transfer parameters
【编程题】【Scratch二级】2019.03 垃圾分类
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
Notice on organizing the second round of the Southwest Division (Sichuan) of the 2021-2022 National Youth electronic information intelligent innovation competition
随机推荐
Kubernetes Static Pod (静态Pod)
LeetCode刷题
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
Solution to the problem of unserialize3 in the advanced web area of the attack and defense world
他们齐聚 2022 ECUG Con,只为「中国技术力量」
Binder核心API
[question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
1293_FreeRTOS中xTaskResumeAll()接口的实现分析
Prompt configure: error: required tool not found: libtool solution when configuring and installing crosstool ng tool
Coindesk comments on the decentralization process of the wave field: let people see the future of the Internet
哪个券商公司开户佣金低又安全,又靠谱
接口测试要测试什么?
51与蓝牙模块通讯,51驱动蓝牙APP点灯
52歲的周鴻禕,還年輕嗎?
Tencent security released the white paper on BOT Management | interpreting BOT attacks and exploring ways to protect
深潜Kotlin协程(二十二):Flow的处理
詹姆斯·格雷克《信息简史》读后感记录
[programming questions] [scratch Level 2] March 2019 garbage classification
Experience of autumn recruitment in 22 years
Database query - what is the highest data?