当前位置:网站首页>【LeetCode】13. Linked List Cycle·环形链表
【LeetCode】13. Linked List Cycle·环形链表
2022-07-28 01:22:00 【AQin1012】
题目描述
英文版描述
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.
英文版地址
https://leetcode.com/problems/linked-list-cycle/
中文版描述
给你一个链表的头节点 head ,判断链表中是否有环。 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。
注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。 如果链表中存在环 ,则返回 true 。 否则,返回 false 。
示例 1:

输入:head = [3,2,0,-4], pos = 1 输出:true 解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:

输入:head = [1,2], pos = 0 输出:true 解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:

输入:head = [1], pos = -1 输出:false 解释:链表中没有环。
提示:
链表中节点的数目范围是 [0, 104]
-105 <= Node.val <= 105
pos 为 -1 或者链表中的一个 有效索引
中文版地址
https://leetcode.cn/problems/linked-list-cycle/
解题思路
逐个遍历,新建Map<NodeList, Interger>用于存储途径的NodeList,Map中没有的就添加进去,有的就返回true,直到遍历结束还有重复的元素,就返回false
解题方法
俺这版

/**
* 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;
}
}
官方版
方法一:哈希表

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;
}
}方法二:快慢指针

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;
}
}边栏推荐
- Lombok prompts variable log error when using JUnit test in idea
- [solution] solve the problem of SSH connection being inactive for a long time and being stuck and disconnected
- 小程序毕设作品之微信校园浴室预约小程序毕业设计成品(3)后台功能
- What can you say to comfort your girlfriend or daughter-in-law
- Promise from introduction to mastery (Chapter 4 async and await)
- Deep Residual Learning for Image Recognition浅读与实现
- 并发编程的三大核心问题(荣耀典藏版)
- Network must know topics
- Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
- Wechat campus bathroom reservation applet graduation design finished product (1) development outline
猜你喜欢

Canvas 从入门到劝朋友放弃(图解版)

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

分层图解决的一些最短路问题

正则表达式

LeetCode 热题 HOT 100 -> 2.两数相加

1313_pyserial的安装以及文档的生成

Appium 点击操作梳理

retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'

JVM tuning -xms -xmx -xmn -xss

With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
随机推荐
【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山
retainface使用报错:ModuleNotFoundError: No module named 'rcnn.cython.bbox'
CeresDAO:Ventures DAO的“新代言”
Feign calls get and post records
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself
Find - block search
数字赋能 创新未来:海丝埃睿迪亮相第五届数字中国建设峰会
这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
第三章 队列
The cooperation between starfish OS and metabell is just the beginning
「冒死上传」Proe/Creo产品结构设计-止口与扣位
[hcip] routing strategy, strategic routing
Unity 保存图片到相册以及权限管理
Achievements in science and Technology (XXVIII)
Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0
Learn this trick and never be afraid to let the code collapse by mistake
软考 --- 数据库(2)关系模型
Digital empowerment and innovation in the future: hese eredi appears at the 5th Digital China Construction Summit
MySQL高可用和主从同步