当前位置:网站首页>力扣141题:环形链表
力扣141题:环形链表
2022-07-26 22:25:00 【瀛台夜雪】
力扣141题:环形链表
题目描述
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
输入输出样例

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

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

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。
解法一:使用hash表存储,空间复杂度为O(n)
bool hasCycle(ListNode *head)
{
//建立hash表
unordered_set<ListNode*>sets;
while(head!=nullptr)
{
//只要有一个已经存在便跳出存在环
if(sets.count(head))
{
return true;
}
sets.insert(head);
head=head->next;
}
return false;
}
解法二,使用快慢指针的思想
//解法二,借用快慢指针思想,若存在环,则两个一定会相遇,即 一个一次移动两步,一个一次移动一步
bool hasCycle(ListNode *head)
{
if(head==nullptr||head->next==nullptr)
{
return false;
}
//定义快慢指针
//设置二者的起点,因为要尽量满足循环条件所以设置二者起点不同,但不影响结果
ListNode *slow=head;
ListNode *fast=head->next;
while(slow!=fast)
{
//当快指针等于nullptr,或则其下一个结点等于nullptr,代表链表一定不存在环
//因为链表的长度是不可提前预知的,并且快指针比慢指针移动的要快
if(fast==nullptr||fast->next==nullptr)
{
return false;
}
//设置二者每次移动的步长
slow=slow->next;
fast=fast->next->next;
}
return true;
}
边栏推荐
- ESMFold: AlphaFold2之后蛋白质结构预测的新突破
- Silicon Valley class lesson 6 - Tencent cloud on demand management module (I)
- How to use data pipeline to realize test modernization
- Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
- HCIA-R&S自用笔记(23)DHCP
- 企业如何缓解物联网和工业物联网安全风险
- What is the reason for oom during redis shake synchronization in shake database?
- Real time voice quality monitoring
- Programmer growth chapter 29: how to motivate employees?
- My SQL is OK. Why is it still so slow? MySQL locking rules
猜你喜欢
电脑开机后内存占用过高(50%以上)

HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理

Hcia-r & s self use notes (21) STP technical background, STP foundation and data package structure, STP election rules and cases

1. Configuration environment and project creation

Real time voice quality monitoring

Hcia-r & s self use notes (19) VLAN configuration and experiment, routing between VLANs

Ribbon负载均衡

KT6368A蓝牙芯片开发注意事项以及问题集锦--长期更新

PostgreSQL and Navicat: the backbone of the database industry
![[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code](/img/11/27d354a411358bfb39ae7126f33a37.png)
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
随机推荐
Distributed lock and its implementation
Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
PostgreSQL and Navicat: the backbone of the database industry
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code
How can enterprises mitigate the security risks of Internet of things and industrial Internet of things
黑马瑞吉外卖之新增员工
第二部分—C语言提高篇_6. 多维数组
华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
Part II - C language improvement_ 8. File operation
Write golang simple C2 remote control based on grpc
After working for one year, I have some insights (written in 2017)
Eureka basic use
Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support
Hcia-r & s self use notes (21) STP technical background, STP foundation and data package structure, STP election rules and cases
Kalibr calibration realsensed435i -- multi camera calibration
Signal debugging document developed by car
Tensorflow2.0 深度学习运行代码简单教程
Typescript stage learning
Concept of functional interface & definition and use of functional interface
Basic use of gateway