当前位置:网站首页>leetcode-141.环形链表
leetcode-141.环形链表
2022-06-11 16:00:00 【LuZhouShiLi】
题目-141.环形链表
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
https://leetcode.cn/problems/linked-list-cycle/
思路
设置快慢指针,设置初始指针slow = fast = head,然后slow = slow.next, fast = fast.next.next;
也就是慢指针走一步,快指针走两步,如果快指针追上慢指针,该链表就是循环链表。
代码
/** * 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) {
ListNode left = head;
ListNode right = head;
while(right != null && right.next != null)
{
left = left.next;
right = right.next.next;
if(left == right)
{
return true;
}
}
// 如果不满足循环条件 出来了 说明一定没有环
return false;
}
}
边栏推荐
- With an average annual salary of 20W, automated test engineers are so popular?
- 如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk
- jdbc调试错误,求指导
- Laravel 8 uses passport for auth authentication and token issuance
- Take you in-depth understanding of AGC cloud database
- 时间复杂度与空间复杂度解析
- Detailed explanation of MySQL binlog log and master-slave replication
- 收藏 | 可解释机器学习发展和常见方法!
- Will you be punished for not wearing seat belts in the back row?
- Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!
猜你喜欢

想学好ArrayList,看完这篇就够了

Open the door of the hybrid cloud market, Lenovo xcloud's way to break the situation

laravel 2020-01-01T00:00:00.000000Z 日期转化

Aaai2022 latest "time series data processing" report, 127 pages of PPT describing time series data processing and medical application progress

Will you be punished for not wearing seat belts in the back row?

Opengauss database flashback function verification

Classmate, have you heard of mot?

Cloud data management will break the island of storage and the island of team

Streaking? Baa!

rf中安装的第四步:安装 robotframework-selenium2library报错
随机推荐
wget命令使用
【愚公系列】2022年06月 .NET架构班 076-分布式中间件 ScheduleMaster的执行原理
PostgreSQL create table
Heartless sword English Chinese bilingual poem 001 Spring outing
Classmate, have you heard of mot?
Learn automatic testing of postman interface from 0 to 1
Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!
[golang] leetcode special training - array and slice
项目经理如何击退被工作汇报支配的恐惧感?
整了20张高清数据分析全知识地图,强烈建议收藏!
[digital signal processing] correlation function (correlation function property | conjugate symmetry property of correlation function | even symmetry of real signal autocorrelation function | conjugat
Nat Commun|语言模型可以学习复杂的分子分布
如何优化 Compose 的性能?通过「底层原理」寻找答案 | 开发者说·DTalk
Laravel 8 realizes database backup through task scheduling
基于ssm框架实现的企业进销存管理系统【源码+数据库+毕设】
laravel 8 通过 任务调度 实现 数据库备份
大龄码农从北京到荷兰的躺平生活
laravel8 实现签到功能案例
Project workspace creation steps - Zezhong ar automated test tool
Db4ai: database driven AI