当前位置:网站首页>力扣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;
}
边栏推荐
- 【flask高级】结合源码分析flask中的线程隔离机制
- org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
- Dao:op token and non transferable NFT are committed to building a new digital democracy
- Application of workflow engine in vivo marketing automation | engine 03
- Part II - C language improvement_ 6. Multidimensional array
- 第二部分—C语言提高篇_6. 多维数组
- 实战项目:Boost搜索引擎
- Pre research of data quality management tools Griffin vs deequ vs great expectations vs quality
- Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library
- How to use data pipeline to realize test modernization
猜你喜欢

华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应

Ribbon load balancing

【2016】【论文笔记】差频可调谐THz技术——

Product principles of non-financial decentralized application

Why am I still writing articles on CSDN? A journey of accompanying learning.

Part II - C language improvement_ 7. Structure

How to use data pipeline to realize test modernization

New employees of black maredge takeout

Pyqt5 how to set pushbutton click event to obtain file address

org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line
随机推荐
Easily implement seckill system with redis! (including code)
Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
Part II - C language improvement_ 5. Bit operation
C language dynamic memory management
MySQL random paging to get non duplicate data
Typescript stage learning
Esmfold: a new breakthrough in protein structure prediction after alphafold2
Security team: Recently, there is an rce vulnerability in the COREMAIL email client of windows, which may lead to the disclosure of the private key of the wallet
PostgreSQL and Navicat: the backbone of the database industry
力扣155题,最小栈
Public cloud security and compliance considerations
Three person management of system design
Vit:vision transformer super detailed with code
Novice online interview [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
Eureka基本使用
Custom type
Boss; Can flick CDC Oracle finish reading the full amount of data, just like directly fetching data from the database
Cloud native microservices Chapter 1 server environment description
Ribbon load balancing
Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective