当前位置:网站首页>Leetcode circular linked list (fast and slow pointer) code line by line interpretation
Leetcode circular linked list (fast and slow pointer) code line by line interpretation
2022-07-02 22:31:00 【ZibeSun】
Circular list
Preface
The judgment of circular linked list is a classical problem in algorithm learning , This article will explain and use the code to explain how to judge the circular linked list through the speed pointer .
subject
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 .
Example :

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 .
Link to the original topic :https://leetcode-cn.com/problems/linked-list-cycle
solution - Speed pointer
Specific ideas : Create two pointers , A quick pointer , A slow pointer , Both pointers initially point to the chain header node . The fast pointer moves back two nodes at a time , The slow pointer moves back one node at a time . If the linked list has a ring , Then the fast and slow hands will meet . If the linked list has no ring , Then the fast pointer will go to the end of the linked list first .
Algorithm flow :
Special judgement : If the chain header node
headNull pointer , Then return directly falseCreate a fast pointer , A slow pointer , Are initialized as chain header nodes
Traversing the linked list , Exit when the fast pointer is empty
The fast pointer goes back one node first , If the node is not empty , Then go back one node
Slow the pointer back one node
If the speed pointer is equal , And are not empty , Indicates that the speed pointer meets , The list has rings , return
true
If you traverse the linked list normally , It indicates that the linked list has no rings , return
false
Code implementation :
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
bool hasCycle(ListNode *head) {
// Special judgement : If the chain header node head Null pointer , Then return directly false
if(head == NULL) return false;
// Create a fast pointer , Initialize to the chain header node
ListNode *fast = head;
// Create a slow pointer , Initialize to the chain header node
ListNode *slow = head;
// Traversing the linked list , Exit when the fast pointer is empty
while(fast != NULL){
// The fast pointer goes back one node first
fast = fast->next;
// If the node is not empty , Then go back one node
if(fast != NULL) fast = fast->next;
// Slow the pointer back one node
slow = slow->next;
// If you traverse the linked list normally , It indicates that the linked list has no rings , return false
if(fast == slow && fast != NULL) return true;
}
// If you traverse the linked list normally , It indicates that the linked list has no rings , return false
return false;
}
};
边栏推荐
- U++ 学习笔记 ----松弛
- Unity3D学习笔记4——创建Mesh高级接口
- Oriental Aesthetics and software design
- Technological Entrepreneurship: failure is not success, but reflection is
- Technical solution of vision and manipulator calibration system
- APP页面分享口令Rails实现
- The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
- ArrayList分析2 :Itr、ListIterator以及SubList中的坑
- What is it that makes you tremble? Those without fans can learn
- [QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
猜你喜欢
![[001] [arm-cortex-m3/4] internal register](/img/49/a0eceac1a67267216dd9b2566033a1.jpg)
[001] [arm-cortex-m3/4] internal register

LandingSite eBand B1冒烟测试用例
![[shutter] shutter resource file use (import resource pictures | use image resources)](/img/e9/94ae2e3ee315f490eb3cf14bcf2e49.jpg)
[shutter] shutter resource file use (import resource pictures | use image resources)

Pointer and string

Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file

《Just because》阅读感受

PIP audit: a powerful security vulnerability scanning tool

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme

Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
随机推荐
Pointer and string
U++ 原始内存 学习笔记
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
2019 Nanchang (relive the classic)
New feature of go1.18: trylock, which has been tossed n times
Kubernetes resource object introduction and common commands (4)
The difference between include < > and include ""
Hanoi Tower problem
Infrastructure is code: a change is coming
Evolution of messaging and streaming systems under the native tide of open source cloud
Five message formats of OSPF
Oriental Aesthetics and software design
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Get off work on time! Episode 6 of Excel Collection - how to split and count document amounts
Unity3D学习笔记4——创建Mesh高级接口
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
《Just because》阅读感受
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
Ransack组合条件搜索实现