当前位置:网站首页>Force buckle 141 Circular linked list
Force buckle 141 Circular linked list
2022-06-22 02:49:00 【SS_ zico】
141. Circular list
Given a linked list , 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 , We use integers pos To indicate where the end of the list is connected to the list ( Index from 0 Start ). If pos yes -1, There are no links in the list . 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 .
Advanced :
You can use O(1)( namely , Constant ) Does memory solve this problem ?
Example 1:
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 .
Example 2:
Input :head = [1,2], pos = 0
Output :true
explain : There is a link in the list , Its tail is connected to the first node .
Example 3:
Input :head = [1], pos = -1
Output :false
explain : There are no links in the list .
Tips :
The number range of nodes in the linked list is [0, 104]
-105 <= Node.val <= 105
pos by -1 Or one of the lists Valid index .
Solution 1 : Speed pointer
Define two pointers When the fast pointer catches up with the slow pointer, it means that the linked list has a ring
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL || head->next == NULL)
return false;
ListNode *low = head;
ListNode *fast = head->next;
// Use while When you cycle, you should put low and fast Set to different do while You don't have to
while(low != fast)
{
if(fast == NULL || fast->next == NULL)
return false;
low = low->next;
fast = fast->next->next;
}
return true;
}
};
Time complexity O(N)
Spatial complexity O(1)
Solution 2 : Hashtable
We traverse each node of the linked list and put it into the hash table Compare nodes , If this node is stored, it is proved that there is a ring , If it traverses to the end, it means there is no ring
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_set<ListNode *>seen;
while(head)
{
if(seen.count(head))
return true;
seen.insert(head);
head = head->next;
}
return false;
}
};
Time complexity O(N)
Spatial complexity O(N)
边栏推荐
- 【8、一维前缀和】
- 【7. 高精度除法】
- DAST black box vulnerability scanner part 4: scanning performance
- 【Percona-Toolkit】系列之pt-table-checksum和pt-table-sync 数据校验修复神器
- File upload vulnerability shooting range analysis upload_ LABS
- 最热门海量的阿里云盘资源分享
- Is the link of Hengtai securities VIP low commission account opening safe?
- 【5. 高精度减法】
- PMP pre exam guide on June 25, you need to do these well
- An article thoroughly learns to draw data flow diagrams
猜你喜欢

Markdown advanced syntax, marktext compatible

EMC radiation emission rectification - principle case analysis

An article thoroughly learns to draw data flow diagrams

Two dot vertical progress styles

Day16QtQLabel2021-10-22

GraphConnect 2022 大会的产品发布一览

国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了

All the knowledge you want to know about the PMP Exam is here
![[9. submatrix sum]](/img/97/32f11e2f26a1f313c808fcc1cd27b3.png)
[9. submatrix sum]

【5. 高精度减法】
随机推荐
In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
Microsoft Internet Explorer was permanently closed on June 15
The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it
Programming of pytorch interface
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
[6. high precision multiplication]
EMC radiation emission rectification - principle case analysis
The brand, products and services are working together. What will Dongfeng Nissan do next?
Day16QtQLabel2021-10-22
[7. high precision division]
Ioerror: no translation files found for default language zh cn Solutions for
mocklog_ Simulation log
PMP pre exam guide on June 25, you need to do these well
EMC輻射發射整改-原理案例分析
GraphAcademy 课程讲解:《Neo4j 图数据科学简介》
Write your own kubernetes controller
【6. 高精度乘法】
Is the link of Hengtai securities VIP low commission account opening safe?
【3.整数与浮点数二分】
DAST black box vulnerability scanner part 4: scanning performance