当前位置:网站首页>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;
}
};
边栏推荐
- Introduction knowledge system of Web front-end engineers
- 1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- 詹姆斯·格雷克《信息简史》读后感记录
- Robomaster visual tutorial (1) camera
- QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
- How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
- Is it safe to open an account on the official website of Huatai Securities?
- 深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
- 【编程题】【Scratch二级】2019.12 绘制十个正方形
猜你喜欢

单机高并发模型设计

搭建ADG过程中复制报错 RMAN-03009 ORA-03113

Daily question brushing record (16)

C language 005: common examples
![[programming questions] [scratch Level 2] March 2019 garbage classification](/img/08/9f7ebf4302c9239784751b579c9efc.png)
[programming questions] [scratch Level 2] March 2019 garbage classification

redis你到底懂不懂之list

从服务器到云托管,到底经历了什么?

The underlying principles and templates of new and delete

After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
![[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game](/img/81/c84432a7d7c2fe8ef377d8c13991d6.png)
[programming problem] [scratch Level 2] 2019.09 make bat Challenge Game
随机推荐
Stm32f1 and stm32cubeide programming example - rotary encoder drive
韦东山第二期课程内容概要
[研发人员必备]paddle 如何制作自己的数据集,并显示。
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Detailed explanation of interview questions: the history of blood and tears in implementing distributed locks with redis
Relevant methods of sorting arrays in JS (if you want to understand arrays, it's enough to read this article)
搭建ADG过程中复制报错 RMAN-03009 ORA-03113
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
攻防世界Web进阶区unserialize3题解
【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
【编程题】【Scratch二级】2019.09 绘制雪花图案
[programming problem] [scratch Level 2] March 2019 draw a square spiral
如果在构造函数中抛出异常,最好的做法是防止内存泄漏?
How can CSDN indent the first line of a paragraph by 2 characters?
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
【编程题】【Scratch二级】2019.03 垃圾分类
Tools for debugging makefiles - tool for debugging makefiles
Open display PDF file in web page
Is 35 really a career crisis? No, my skills are accumulating, and the more I eat, the better
Linkedblockingqueue source code analysis - add and delete