当前位置:网站首页>【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;
}
}边栏推荐
- 获取两个集合相差数据
- ArcGIS: loading historical remote sensing images
- How MySQL uses indexes (glory Collection Edition)
- Plato Farm在Elephant Swap上铸造的ePLATO是什么?
- OBS keyboard plug-in custom DIY
- [advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control
- Get the difference data of two sets
- Flume (5 demos easy to get started)
- Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
- 关于Sqli-labs单引号不报错的问题
猜你喜欢

How is insert locked in MySQL? (glory Collection Edition)

程序里随处可见的interface,真的有用吗?真的用对了吗?

Unity saves pictures to albums and rights management

「冒死上传」Proe/Creo产品结构设计-止口与扣位

With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it

Aike AI frontier promotion (7.14)

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

【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例

Explore flex basis

Wechat campus bathroom reservation applet graduation design finished product (1) development outline
随机推荐
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(3)后台功能
0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
11 Django basics database operation
pytorch优化器设置
「冒死上传」Proe/Creo产品结构设计-止口与扣位
feign调用get和post记录
基于stm32的恒功率无线充电
Plato Farm在Elephant Swap上铸造的ePLATO是什么?
Unity 保存图片到相册以及权限管理
组原必备知识点
Lombok prompts variable log error when using JUnit test in idea
Feign calls get and post records
[Star Project] small hat aircraft War (VI)
MYSQL解决死锁之路 - 常见 SQL 语句的加锁分析
正则表达式
CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议
获取两个集合相差数据
网络必知题目
C # introducing WinAPI to pass the character set of Chinese string parameters
cn+dt