当前位置:网站首页>[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;
}
}边栏推荐
- [data processing] boxplot drawing
- Unity 保存图片到相册以及权限管理
- 「冒死上传」Proe/Creo产品结构设计-止口与扣位
- Find - block search
- Leetcode hot topic Hot 100 - > 3. longest substring without repeated characters
- 软件产品第三方测试费用为什么没有统一的报价?
- 【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
- 正则表达式
- Use try-with-resources or close this
- When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way
猜你喜欢

第二季度邮件安全报告:邮件攻击暴增4倍,利用知名品牌获取信任

Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice

The virtual host website cannot access the self-test method

Explore flex basis

Wechat campus bathroom reservation applet graduation design finished product (3) background function

Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance

Cesium3Dtilesets 使用customShader的解读以及泛光效果示例

别人发你的jar包你如何使用(如何使用别人发您的jar包)

OBS keyboard plug-in custom DIY

MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
随机推荐
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
"The faster the code is written, the slower the program runs"
Usage of delegate
Use try-with-resources or close this
Leetcode hot topic Hot 100 - > 2. Add two numbers
JVM tuning -xms -xmx -xmn -xss
How is insert locked in MySQL? (glory Collection Edition)
MySQL high availability and master-slave synchronization
mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
树的孩子兄弟表示法
智能合约安全——selfdestruct攻击
Necessary knowledge points of the original group
程序里随处可见的interface,真的有用吗?真的用对了吗?
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
What problems should be avoided when using BigDecimal type? (glory Collection Edition)
借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析
Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)
Unity saves pictures to albums and rights management
Hardware standard