当前位置:网站首页>Niuke bm6 judges whether there is a ring in the linked list
Niuke bm6 judges whether there is a ring in the linked list
2022-07-21 03:09:00 【Doraemon 0219】
describe
Judge whether there is a ring in a given linked list . If yes, otherwise return true, Otherwise return to false.
Data range : Chain length 0≤n≤10000, The value of any node in the linked list satisfies ∣val∣<=100000
requirement : Spatial complexity O(1), Time complexity O(n).
For example, the input {3,2,0,-4} when , The corresponding linked list structure is shown in the figure below :
It can be seen that the entry node of the ring is the... From the beginning node 1 Nodes ( notes : The first node is 0 Nodes ), So the output true.
The initial definition of the structure is as follows :
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
Solution 1 Hashtable
1.set Hashtable
class Solution
{
public:
bool hasCycle(ListNode *head)
{
set<ListNode*> s;
while(head!=NULL)
{
if(s.find(head)!=s.end())
return true;
else
s.insert(head);
head=head->next;
}
return false;
}
};
2.map Hashtable
class Solution
{
public:
bool hasCycle(ListNode *head)
{
map<ListNode*,bool> m;
while(head!=NULL)
{
if(m.find(head)!=m.end())
return true;
else
m[head]=true;// Add key value
head=head->next;
}
return false;
}
};
The above two methods are essentially the same . All use hash table to judge whether there is a ring in the linked list .
Solution 2 Fast slow double pointer
Using two Pointers ,fast and slow, Their starting position is the head of the linked list , And then ,slow The pointer moves back one position at a time , and fast Move back two positions at a time , If there are rings in the list , Then these two pointers will eventually meet again .
class Solution
{
public:
bool hasCycle(ListNode* head)
{
ListNode *slow = head, *fast = head;
while ( fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return true;
}
return false;
}
};
Solution 3 Soul solution ..( Not recommended )
The title says n The range is 0 To 10000, So if the number of cycles is greater than 10000 Does it mean there must be a ring , On the contrary, if the cycle ends after a certain number of times, it means that it must be acyclic ...
class Solution {
public:
bool hasCycle(ListNode *head) {
int sum=0;// The title says n The range is 0 To 10000, So if the number of cycles is greater than 10000 Does it mean there must be a ring , On the contrary, if the cycle ends after a certain number of times, it means that it must be acyclic
while(head)
{
head=head->next;
sum++;
if(sum>10000)
return true;
}
return false;
}
};
边栏推荐
- 【科学文献计量】关键词的挖掘与可视化
- Baidu flying paste application running on embedded ARM
- J9数字平台论:元宇宙中DeFi的可能性和局限性
- MATLAB数字图像处理 实验五:形态学图像处理
- Apache Doris rapid deployment operation and maintenance guide based on ansible
- Doris connector and Flink CDC realize accurate access to MySQL database and table exactly once
- 第11章 网络物理隔离技术原理与应用
- 解决方案:业主单位智慧工地监管云平台
- Vben admin time selector related configuration and setting of unselectable time
- 化工企业如何选择双重预防机制数字化服务商
猜你喜欢
An interesting example to illustrate the difference of emplace_ back() and push_ back()
ES6中Symbol、迭代器和生成器基本语法
Technical dry goods | mindspire self-developed high-order optimizer source code analysis and practical application
[note] logstash environment setup and installation configuration
MATLAB数字图像处理 实验五:形态学图像处理
记笔记之Hal库串口
Developers share the second regression analysis of "Book Eating bar: deep learning and mindspire practice"
Apache Doris Binlog Load使用方法及示例
55k稳了,推荐系统永远滴神!
【微信小程序】文本域输入带最大字数限制(1/100)
随机推荐
【科学文献计量】关键词的挖掘与可视化
[scientific literature measurement] statistics and visualization of word number and frequency of Chinese and English literature titles and abstracts
使用renren-generator逆向生成CRUD代码
Constructeur de liste STL, taille
win10基于IDEA,搭建Presto开发环境
Apache Doris Oracle ODBC外表使用指南
MATLAB数字图像处理 实验五:形态学图像处理
Deep learning 2-linear unit and gradient descent
TASK02|EDA初体验
Flink Doris Connector设计方案
Operation of STL Vector
QT quick 3D physics in QT 6.4
How to choose data application development language and environment
CTFshow-web入门信息收集-wp(1-20) (详解)
Do you dare to use BigDecimal without mastering these pits?
Densenet learning notes (core vs. RESNET):
Developers share the second regression analysis of "Book Eating bar: deep learning and mindspire practice"
Apache Doris ODBC外表之Postgresql使用指南
Apache Doris ODBC Mysql外表在Ubuntu下使用方法及配置
webSocket學習與使用