当前位置:网站首页>[summary of linked list skills] 141. Circular linked list (simple)
[summary of linked list skills] 141. Circular linked list (simple)
2022-07-22 19:00:00 【Sand is sand】
One 、 subject
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
Two 、 Problem solving
/**
* 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) {
ListNode* fast = head;
ListNode* slow = head;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
};
author :shazi2925
link :https://leetcode-cn.com/problems/linked-list-cycle/solution/huan-xing-lian-biao-jian-dan-by-shazi292-mijg/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- There is no session in the tensorflow module
- 1. MySQL null and in; What is 2.127.0.0.2?
- Qt | boîtes de dialogue modales et non modales qdialog
- 1. Qtimer:: how to transfer parameters from singleshot, 2. Qmetaobject:: invokemethod how to transfer values with functions
- PTA exercise 8-8 judging palindrome string
- 1.针对QDate()的日期指向那边, 2.QT_VERSION的用法总结
- QT | modal dialog and modeless dialog qdialog
- pytorch自定义dataloder的时候,返回参数
- Qt | 模態對話框和非模態對話框 QDialog
- After the project is started, the mapper XML file is loaded circularly
猜你喜欢
VLFeat、pydot配置
1. Access JSON in a way similar to the window path
Upgrade win10sp1 to the latest version; Qt5.9.6 static compilation (network is valid)
Caching-sha2-password problem occurred when connecting mysql8.0
Flink学习笔记(六)Flink的时间和窗口
fucking-algorithm
1. The solution of line feed qt5- "vs" in constants; 2. Problems and solutions of common compilation of QT and vs of the same code
leetCode笔记
Flink learning notes (IV) Flink runtime architecture
Stm32+esp8266+mqtt protocol connects onenet IOT platform
随机推荐
Solve the problem that the target bytecode version number of IntelliJ cannot be maintained
Caching-sha2-password problem occurred when connecting mysql8.0
PTA basic question 7-23 currency conversion (20 points) (true)
程序员面试金典面试题 01.03. URL化
国内 Ngrok 实现内网穿透
Summary 20220209
Leetcode 2039. when the network is idle
Leetcode 653. sum of two IV - input BST
Leetcode 654. maximum binary tree
Qt | 模态对话框和非模态对话框 QDialog
Flink学习笔记(三)Flink安装部署
IP地址、CIRD格式网址、主机名正则表达式
Ways of data optimization
NRF24L01 wireless module setting transmit receive mode method
项目启动过后,一直循环加载mapper xml文件
实现各个微服务间限制IP访问 的三种方式
Go language learning: go language journey - exercise questions and reference answers
1.雷点:mysql数据库转移到oracle,2.qt5.12.3 连接oracle 12c数据库
MNIST手写数字识别案例TensorFlow 2.0 实践
Latex如何写引用时将作者名字缩写为et al