当前位置:网站首页>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;
}
};
边栏推荐
- Market Research - current market situation and future development trend of handheld wound imaging equipment
- 【C 题集】of Ⅴ
- 数据库系统概论第一章简答题-期末考得怎么样?
- Market Research - current situation and future development trend of herringbone gear Market
- Image segmentation using pixellib
- UE4 游戏架构 学习笔记
- Unity3D学习笔记4——创建Mesh高级接口
- UE4 UI自适应屏幕
- 一周生活
- LxC terminal login method
猜你喜欢

Gee: (II) resampling the image
![[shutter] shutter gesture interaction (small ball following the movement of fingers)](/img/5a/a8dad8a0943645c980cc4fe7cb55d4.gif)
[shutter] shutter gesture interaction (small ball following the movement of fingers)

图像基础概念与YUV/RGB深入理解

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!

Official announcement! The golden decade of new programmers and developers was officially released

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

情感计算与理解研究发展概述

Kubernetes resource object introduction and common commands (4)
![[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)](/img/4c/c8dae41fc2eb18b5153cf36861fc7d.jpg)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)

From "bronze" to "King", there are three secrets of enterprise digitalization
随机推荐
#include errors detected. Please update your includePath.
Five message formats of OSPF
[QT] QT multithreading development - four methods to realize multithreading design
VIM command-t plugin error: unable to load the C extension - VIM command-t plugin error: could not load the C extension
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Official announcement! The golden decade of new programmers and developers was officially released
100 important knowledge points that SQL must master: management transaction processing
PIP audit: a powerful security vulnerability scanning tool
如何访问kubernetes API?
Image segmentation using pixellib
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
Daily book - low code you must understand in the era of digital transformation
Basic concepts of image and deep understanding of yuv/rgb
Destroy in beforedestroy invalid value in localstorage
Web侧防御指南
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Perceptron model and Application
[QT] Q multithreaded development - Analysis of multithreaded application examples (Mandelbrot)
Pointer and string
Ransack combined condition search implementation