当前位置:网站首页>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
head
Null 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;
}
};
边栏推荐
- [shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
- Ransack组合条件搜索实现
- Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
- Introduction to the principle of geographical detector
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- 在beforeDestroy中销毁localStorage中的值无效
- Daily book -- analyze the pain points of software automation from simple to deep
- scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
- :last-child 不生效解决
- VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
猜你喜欢
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
[shutter] shutter gesture interaction (small ball following the movement of fingers)
TinyMCE visual editor adds Baidu map plug-in
Evolution of messaging and streaming systems under the native tide of open source cloud
《Just because》阅读感受
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
【ODX Studio编辑PDX】-0.1-如何快速查看各Variant变体间的支持的诊断信息差异(服务,Sub-Function...)
Daily book CSO advanced road first exposed
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
随机推荐
From "bronze" to "King", there are three secrets of enterprise digitalization
Pointer array parameter passing, pointer parameter passing
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Etcd raft protocol
Introduction to victoriametrics
Socket套接字C/S端流程
About test cases
Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
Market Research - current market situation and future development trend of high tibial osteotomy plate
20220702-程序员如何构建知识体系?
How do I access the kubernetes API?
scrcpy这款软件解决了和同事分享手机屏幕的问题| 社区征文
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Bridge emqx cloud data to AWS IOT through the public network
[shutter] shutter resource file use (import resource pictures | use image resources)
An overview of the development of affective computing and understanding research
使用 EMQX Cloud 实现物联网设备一机一密验证
Error in PIP installation WHL file: error: is not a supported wheel on this platform
It's not easy to say I love you | use the minimum web API to upload files (swagger support) # yyds dry inventory #
:last-child 不生效解决