当前位置:网站首页>[leetcode] 13. linked list cycle · circular linked list
[leetcode] 13. linked list cycle · circular linked list
2022-07-28 02:37:00 【AQin1012】
Title Description
Description in English
Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle in the linked list. Otherwise, return false.
English address
https://leetcode.com/problems/linked-list-cycle/
Description of Chinese version
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 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
Address of Chinese version
https://leetcode.cn/problems/linked-list-cycle/
Their thinking
One by one , newly build Map<NodeList, Interger> For storage path NodeList,Map Add what is not in , If you have something, go back true, There are repeated elements until the end of traversal , Just go back to false
How to solve the problem
My version

/**
* Definition for singly-linked list.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
Map<ListNode, Integer> map = new HashMap<>();
int index = 0;
while (head != null) {
if (map.containsKey(head)) {
return true;
} else {
map.put(head, index);
index++;
head = head.next;
}
}
return false;
}
}
Official edition
Method 1 : Hashtable

public class Solution {
public boolean hasCycle(ListNode head) {
Set<ListNode> seen = new HashSet<ListNode>();
while (head != null) {
if (!seen.add(head)) {
return true;
}
head = head.next;
}
return false;
}
}Method 2 : Speed pointer

public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}边栏推荐
- Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
- Use of Day6 functions and modules
- AWS elastic three swordsman
- Deep Residual Learning for Image Recognition浅读与实现
- How is insert locked in MySQL? (glory Collection Edition)
- 【自我成长网站收集】
- Newline required at end of file but not found.
- What problems should be avoided when using BigDecimal type? (glory Collection Edition)
- Maskedauutoencoders visual learner cvpr2022
- MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
猜你喜欢

How MySQL uses indexes (glory Collection Edition)

Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)

新基建助力智能化道路交通领域的转型发展

The virtual host website cannot access the self-test method

MySQL 中的 INSERT 是怎么加锁的?(荣耀典藏版)

Should programmers choose outsourcing companies
![This operation may not be worth money, but it is worth learning | [batch cutting of pictures]](/img/e8/a34e471b0089f8085b140c74b5c01f.jpg)
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]

Notes for the fourth time of first knowing C language

Say yes, I will love you, and I will love you well

Use try-with-resources or close this
随机推荐
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
基于stm32的恒功率无线充电
Get the difference data of two sets
Unity 保存图片到相册以及权限管理
OBS keyboard plug-in custom DIY
【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)
MySQL锁系列之锁算法详解(荣耀典藏版)
Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0
[data processing] boxplot drawing
2022.7.8 supplement of empty Luna
【HCIP】BGP 特性
windbg
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself
ps 简单使用
软工必备知识点
MySQL是如何利用索引的(荣耀典藏版)
Canonical Address
Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)
数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会