当前位置:网站首页>Question 142 of Li Kou: circular linked list 2
Question 142 of Li Kou: circular linked list 2
2022-07-23 14:31:00 【Yingtai night snow】
Power button 142 topic : Circular list 2
Title Description
Give you a list of the head node head , Judge whether there are links in the list .
If there is a node in the linked list , It can be done by continuously tracking next The pointer reaches again , Then there is a ring in the linked list . To represent a ring in a given list , The evaluation system uses an integer pos To indicate where the end of the list is connected to the list ( Index from 0 Start ). Be careful :pos Not passed as an argument . Just to identify the actual situation of the linked list .
If there are rings in the list , Then return to true . otherwise , return false .
I/o sample

Input :head = [3,2,0,-4], pos = 1
Output : The return index is 1 The linked list node of
explain : There is a link in the list , Its tail is connected to the second node .

Input :head = [1,2], pos = 0
Output : The return index is 0 The linked list node of
explain : There is a link in the list , Its tail is connected to the first node .

Input :head = [1], pos = -1
Output : return null
explain : There are no links in the list .
Solution 1 : Use hash Table is stored , The space complexity is O(n)
// Use hash Table to establish the traversal relationship between nodes
ListNode *detectCycle(ListNode *head)
{
unordered_set<ListNode *>sets;
while(head!=nullptr)
{
if(sets.count(head))
{
return head;
}
else{
sets.insert(head);
head=head->next;
}
}
return nullptr;
}
Solution 2 , The thought space complexity of using the fast and slow pointer is O(n)
// Use the idea of fast and slow pointer to complete
// The node passed by the slow pointer : Outside the ring a , In loop displacement b, Residual displacement in the ring c, Total number of moves n Total mobile nodes a+n(b+c)+b
// Fast pointer to the node : The speed of the fast pointer is twice that of the slow pointer , But the number of knots is the same (a+b)*2=(a+n(b+c)+b)
//a+b=n(b+c) a=(n-1)b+nc a=(n-1)(b+c) +c
// With n=1 Value
// a=c
ListNode *detectCycle(ListNode *head)
{
ListNode *fast=head;
ListNode *slow=head;
while(fast!=nullptr &&fast->next!=nullptr)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
ListNode *index1=fast;
ListNode *index2=head;
//a=c
//n=1, Only over 1 circle
while(index1!=index2)
{
index1=index1->next;
index2=index2->next;
}
// Return to the intersection
return index2;
}
}
return nullptr;
}
边栏推荐
- 【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
- Fabric. JS basic brush
- ArcGIS使用DEM数据划定汇水区具体步骤过程
- 链下数据互操作
- JS to obtain age through ID card
- 【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
- Description of test platform and hardware design
- 【模电复习——二极管】
- Excitation generator, monitor
- Pbootcms数据库转换教程(sqlite转mysql详细教程)
猜你喜欢

ThreadLocal interview Kills 11 consecutive questions

Day 5 experiment

Okrk3399 Development Board reserves i2c4 to mount EEPROM
![[download attached] several scripts commonly used in penetration testing that are worth collecting](/img/01/3b74c5ab4168059827230578753be5.png)
[download attached] several scripts commonly used in penetration testing that are worth collecting

Surrounded Regions

JS software unloading prompt expression changes with the mouse JS special effect

How can manual testing turn to automated testing? Byte 5 years of automation experience talk about

JS texture style pie chart plug-in

338. 比特位计数

581. 最短无序连续子数组
随机推荐
太平洋大西洋水流问题
NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘
Experience in developing large crawlers in requests Library
Flat style feedback form page
Fastadmin changes the pop-up size of the default table button
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
Design instantiation and connection
c语言实现strcmp、strstr、strcat、strcpy
CPU, memory, disk speed comparison
antd form表单——重置方法不生效——基础积累——prop的重要性
How many processors is Tianji 720 equivalent to Xiaolong? How about Tianji 720 equivalent to Xiaolong
寻找峰值[抽象二分练习]
优化华为云服务器采用Key登陆
LZ77文件压缩
JS software unloading prompt expression changes with the mouse JS special effect
Tensor, numpy, PIL format conversion and image display
JS to obtain age through ID card
js纹理样式饼图插件
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...