当前位置:网站首页>Question 141 of Li Kou: circular linked list
Question 141 of Li Kou: circular linked list
2022-07-26 23:38:00 【Yingtai night snow】
Power button 141 topic : Circular list
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 :true
explain : There is a link in the list , Its tail is connected to the second node .

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

Input :head = [1], pos = -1
Output :false
explain : There are no links in the list .
Solution 1 : Use hash Table is stored , The space complexity is O(n)
bool hasCycle(ListNode *head)
{
// establish hash surface
unordered_set<ListNode*>sets;
while(head!=nullptr)
{
// As long as one already exists, it will jump out of the ring of existence
if(sets.count(head))
{
return true;
}
sets.insert(head);
head=head->next;
}
return false;
}
Solution 2 , The idea of using fast and slow pointers
// Solution 2 , Borrow the idea of speed pointer , If there is a ring , Then the two will meet , namely Move two steps at a time , Move one step at a time
bool hasCycle(ListNode *head)
{
if(head==nullptr||head->next==nullptr)
{
return false;
}
// Define speed pointer
// Set the starting point of both , Because we should try to meet the cycle conditions, the starting points of the two are different , But it doesn't affect the results
ListNode *slow=head;
ListNode *fast=head->next;
while(slow!=fast)
{
// When the fast pointer equals nullptr, Or its next node is equal to nullptr, It means that there must be no ring in the linked list
// Because the length of the linked list is unpredictable , And the fast pointer moves faster than the slow pointer
if(fast==nullptr||fast->next==nullptr)
{
return false;
}
// Set the step size of each movement
slow=slow->next;
fast=fast->next->next;
}
return true;
}
边栏推荐
- Three person management of system design
- Re understand the life world and ourselves
- Science | University of Washington uses AI and structural prediction to design new proteins
- 2022年物联网行业有哪些用例?
- HCIA-R&S自用笔记(21)STP技术背景、STP基础和数据包结构、STP选举规则及案例
- 数据供应链的转型 协调一致走向成功的三大有效策略
- Lesson 2 of Silicon Valley classroom - building project environment and developing lecturer management interface
- Part II - C language improvement_ 8. File operation
- Part II - C language improvement_ 6. Multidimensional array
- Basic operations of objects
猜你喜欢

SQL Basics

np. transpose & np.expand_ dims

关于 StatefulWidget,你不得不知道的原理和要点!

买不到的数目

华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应

Science | University of Washington uses AI and structural prediction to design new proteins

华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析

Sign up now | frontier technology exploration: how to make spark stronger and more flexible

第二部分—C语言提高篇_9. 链表

Public cloud security and compliance considerations
随机推荐
Real time voice quality monitoring
Practical project: boost search engine
18. Opening and saving file dialog box usage notes
Kalibr calibration realsensed435i -- multi camera calibration
【C语言】经典的递归问题
8 other programming languages -- Recording
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
如何使用数据管道实现测试现代化
Silicon Valley class lesson 6 - Tencent cloud on demand management module (I)
Kingbasees SQL language reference manual of Jincang database (3.1.1.3. currency type)
What are the use cases in the Internet of things industry in 2022?
Disk expansion process and problems encountered in the virtual machine created by VMWare
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
科研太忙无法顾家?陈婷:人生不能只有一个支点
An online accident, I suddenly realized the essence of asynchrony
[postgresql]postgresqlg use generate_ Series() function completes statistics
公有云安全性和合规性方面的考虑事项
【面试:并发篇26:多线程:两阶段终止模式】volatile版本
Typescript stage learning
Tensorflow2.0 深度学习运行代码简单教程