当前位置:网站首页>Force buckle 142 Circular linked list II
Force buckle 142 Circular linked list II
2022-06-22 02:49:00 【SS_ zico】
The entry node of a link in a list
Cattle guest :NC3
Given a linked list , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
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 It's just for identifying rings , It's not passed to the function as an argument .
explain : It is not allowed to modify the given linked list .
Advanced :
Can you use O(1) Space to solve this problem ?
Method 1 : Hashtable
Ideas and algorithms
A very intuitive idea is : We iterate through each node in the linked list , And write it down ; Once you encounter a node that has been traversed before , You can determine that there is a link in the linked list . With the help of hash table, it is very convenient to realize .
Code
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_set<ListNode *> visited;
while (head != nullptr) {
if (visited.count(head)) {
return head;
}
visited.insert(head);
head = head->next;
}
return nullptr;
}
};
Complexity analysis
Time complexity :O(N), among N Is the number of nodes in the linked list . We just need to access every node in the linked list .
Spatial complexity :O(N), among N Is the number of nodes in the linked list . We need to save each node in the linked list in the hash table .
Method 2 : Speed pointer
Ideas and algorithms
- The speed pointer determines whether there is a ring
One pointer at a time +1; One pointer at a time +2, If we meet , There are rings . - Directly determine the entrance location
Tell me where the pointer gets its head , The slow pointer is the last position ( Meeting point ) Then define ptr Every time +1,
Meeting again is the entrance
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast != nullptr) {
slow = slow->next;
if (fast->next == nullptr) {
return nullptr;
}
fast = fast->next->next;
if (fast == slow) {
ListNode *ptr = head;
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return nullptr;
}
};
Complexity analysis
Time complexity :O(N), among N Is the number of nodes in the linked list . In the initial judgment of whether the fast and slow pointers meet ,slow The distance traveled by the pointer will not exceed the total length of the linked list ; Then when looking for the entry point , The distance traveled will not exceed the total length of the linked list . therefore , The total execution time is O(N)+O(N)=O(N).
Spatial complexity :O(1). Use only the slow,fast,ptr Three pointers .
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/huan-xing-lian-biao-ii-by-leetcode-solution/
边栏推荐
- 【一起上水硕系列】Day Two
- 使用 Neo4j 沙箱学习 Neo4j 图数据科学 GDS
- Try catch of Bash
- Zap grammar sugar
- Ioerror: no translation files found for default language zh cn Solutions for
- What programming does a child learn?
- Fabric. JS iText set italics manually
- Openjudge noi 1.13 46: octal to decimal
- Two dot vertical progress styles
- Graphacademy course explanation: Fundamentals of neo4j graph data science
猜你喜欢
![[1. quick sort]](/img/3d/66ce309761d0d79a5d09718a67def8.png)
[1. quick sort]

File upload vulnerability shooting range analysis upload_ LABS

Creating and extending XFS file system based on LVM
![[7. high precision division]](/img/d9/060cf961db7414b2900ba76b5d8a52.png)
[7. high precision division]
![[9. submatrix sum]](/img/97/32f11e2f26a1f313c808fcc1cd27b3.png)
[9. submatrix sum]

【3.整数与浮点数二分】

rt_ Message queue of thread

最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA

Use of day19qpushbutton 2021-10-30

C++ primer Chapter 2 summary of variables and basic types
随机推荐
The latest official product of domestic brand oppo! This ppt report! It really refreshes my understanding of it
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
【一起上水硕系列】Day Two
Penetration testing - logic vulnerability topic
Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition
JS special effects in the construction of animated web pages
Flash back when GoLand starts
最热门海量的阿里云盘资源分享
Wechat applet film and television comment exchange platform system graduation design (3) background function
Game jam development cycle
[Shangshui Shuo series] day two
Relative references must start with either “/“, “./“, or “../“.
Day17QtQLcdNumber2021-10-22
EMC整改小技巧
Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder
How to apply PMP project management knowledge?
【Docekr学习遇到的坑】
Day21qt mouse event 2021-11-01
June25,2022 PMP Exam clearance manual-3
Write the processing framework for playing