当前位置:网站首页>[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;
}
}边栏推荐
- 树的孩子兄弟表示法
- OBS键盘插件自定义diy
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
- 【自我成长网站收集】
- Ceresdao: new endorsement of ventures Dao
- Compile and use Qwt in qt|vs2017
- Canvas 从入门到劝朋友放弃(图解版)
- 一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
- How MySQL uses indexes (glory Collection Edition)
- 0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
猜你喜欢

Find - block search

Ceresdao: new endorsement of ventures Dao

Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"

"Risking your life to upload" proe/creo product structure design - seam and buckle
![[机缘参悟-53]:阳谋立身,阴谋防身](/img/93/2f61993770d93d9adc80a9fa89e71c.jpg)
[机缘参悟-53]:阳谋立身,阴谋防身

Which users are suitable for applying for rapidssl certificate

Mysql Explain 详解(荣耀典藏版)

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(2)小程序功能

修改MySQL密码的四种方法(适合初学者)

JVM tuning -xms -xmx -xmn -xss
随机推荐
[hcip] BGP features
Network must know topics
Use try-with-resources or close this
实际工作中,我是如何使用 Postman 做接口测试?
Should programmers choose outsourcing companies
MySQL数据库InnoDB存储引擎中的锁机制(荣耀典藏版)
The virtual host website cannot access the self-test method
作业7.27 IO进程
初识C语言 -- 操作符和关键字,#define,指针
【自我成长网站收集】
Usage of delegate
怎么简单实现菜单拖拽排序的功能
regular expression
[in depth study of 4g/5g/6g topic -42]: urllc-14 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -8-low delay technology-2-slot based scheduling and
What can you say to comfort your girlfriend or daughter-in-law
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]
Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
Eredi reappeared at the digital China Summit and continued to deepen the protection of green waters and mountains with science and technology
功能测试和非功能测试区别简析,上海好口碑软件测试公司推荐