当前位置:网站首页>leetcode-141. Circular linked list
leetcode-141. Circular linked list
2022-06-11 16:11:00 【LuZhouShiLi】
subject -141. Circular list
Give you a list of the head node head , Judge whether there are links in the list .
If there is a node in the linked list , It can be done by continuously tracking next The pointer reaches again , Then there is a ring in the linked list . To represent a ring in a given list , The evaluation system uses an integer pos To indicate where the end of the list is connected to the list ( Index from 0 Start ). Be careful :pos Not passed as an argument . Just to identify the actual situation of the linked list .
If there are rings in the list , Then return to true . otherwise , return false .
https://leetcode.cn/problems/linked-list-cycle/
Ideas
Set speed pointer , Set the initial pointer slow = fast = head, then slow = slow.next, fast = fast.next.next;
That is to say, slow down the pointer by one step , Let's go two steps , If the fast pointer catches up with the slow pointer , The linked list is a circular linked list .
Code
/** * 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;
}
}
// If the cycle conditions are not met Come out It means there must be no ring
return false;
}
}
边栏推荐
- [digital signal processing] correlation function (correlation function property | conjugate symmetry property of correlation function | even symmetry of real signal autocorrelation function | conjugat
- laravel 监听模式
- Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
- [LeetCode每日一题] |686.重复叠加字符串匹配
- Customized thread communication (lock) of JUC
- How to manage concurrent write operations? Get you started quickly
- Yef 2022 opened yesterday. The whole process of free live broadcast on multiple network platforms opened an online technology feast!
- List和Dict数据类型作用详解
- Selenium-- display waiting (medium) -- detailed explanation
- Opengauss AI capability upgrade to create a new AI native database
猜你喜欢

真香,华为主动离职也给 N+1

Opengauss database ODBC environment connection configuration (Windows)

List和Dict数据类型作用详解

Why are bugs changing more and more?

电脑下面的任务栏怎么显示打开的程序

What if the win10 security center cannot be closed

Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
Detailed explanation of MySQL binlog log and master-slave replication

Using cloud DB to build apps quick start - quick games

MySQL快速入门实例篇(入内不亏)
随机推荐
PostgreSQL create table
[Yugong series] June 2022 Net architecture class 079 cluster principle of distributed middleware schedulemaster
What if the win10 security center cannot be closed
Learn how to parse SQL from kernel code
AI function of cutting-edge technology exploration: slow SQL discovery
面试高频算法题---最长回文子串
[digital signal processing] correlation function (correlation function property | conjugate symmetry property of correlation function | even symmetry of real signal autocorrelation function | conjugat
Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
Talk about data center network again
Maui introductory tutorial series (1. framework introduction)
Opengauss database ODBC environment connection configuration (Windows)
Operation guide | how to select a collector on moonbeam and Moonriver
Easy to use GS_ Dump and GS_ Dumpall command export data
Step 4 of installation in RF: an error is reported when installing the robotframework-selenium 2library
laravel 2020-01-01T00:00:00.000000Z 日期转化
TC8:UDP_MessageFormat_01-02
Using cloud DB to build app quick start - quick application
Why are bugs changing more and more?
postgresql创建表
干掉 Swagger UI,这款神器更好用、更高效!