当前位置:网站首页>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;
}
};
边栏推荐
- The underlying principles and templates of new and delete
- Teach you to make a custom form label by hand
- Reading notes 004: Wang Yangming's quotations
- Robomaster visual tutorial (1) camera
- 某马旅游网站开发(登录注册退出功能的实现)
- 玩转Sonar
- Open display PDF file in web page
- How does the markdown editor of CSDN input mathematical formulas--- Latex syntax summary
- Scrapy framework
- QT establish signal slots between different classes and transfer parameters
猜你喜欢
C language 005: common examples
某马旅游网站开发(对servlet的优化)
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
Daily question brushing record (16)
80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
赞!idea 如何单窗口打开多个项目?
QT establish signal slots between different classes and transfer parameters
ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game
【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
随机推荐
5G NR 系统消息
Database query - what is the highest data?
接口测试进阶接口脚本使用—apipost(预/后执行脚本)
测试流程不完善,又遇到不积极的开发怎么办?
The difference between get and post
Development of a horse tourism website (realization of login, registration and exit function)
80% of the people answered incorrectly. Does the leaf on the apple logo face left or right?
Usage of limit and offset (Reprint)
The method of server defense against DDoS, Hangzhou advanced anti DDoS IP section 103.219.39 x
[basis of recommendation system] sampling and construction of positive and negative samples
哪个券商公司开户佣金低又安全,又靠谱
【编程题】【Scratch二级】2019.03 垃圾分类
52歲的周鴻禕,還年輕嗎?
After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
When creating body middleware, express Is there any difference between setting extended to true and false in urlencoded?
Common selectors are
LeetCode刷题
35岁真就成了职业危机?不,我的技术在积累,我还越吃越香了
Development of a horse tourism website (optimization of servlet)
如果在构造函数中抛出异常,最好的做法是防止内存泄漏?