当前位置:网站首页>141. Circular linked list
141. Circular linked list
2022-07-24 01:30:00 【ZNineSun】
Clock in !!! A daily topic
Today I want to share with you a topic of linked list type , The subject is very simple , It is mainly the method of treatment , There are two main questions in this paper , Take you from the first question to the second question .
subject 1:141. Circular list
Title Description :
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 .
Title Example :
Let's simply analyze this problem , The meaning of the topic is very simple , Is to let you judge whether there is a ring , Our approach is also very simple , Save the nodes we have traversed , Once a node is found Next Have been interviewed , Description ring , If the linked list traversal ends, there is no ring .
Conditions for the end of traversal :
- 1. End of traversal , That is to say, the next It's empty .
- 2. Traverse to the node that has been visited .
public boolean hasCycle(ListNode head) {
if(head==null)return false;
List<ListNode>list=new ArrayList<>();
while(!list.contains(head)&&head!=null){
list.add(head);
head=head.next;
}
if(head==null){
return false;
}else return true;
}
Is it simple , Let's continue to think about an advanced problem .
subject 2:142. Circular list 2
Title Description :
Given the head node of a linked list head , Return to the first node of the link where the list begins to enter . If the list has no links , Then return to null.
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 ). If pos yes -1, There are no links in the list . Be careful :pos Not passed as an argument , Just to identify the actual situation of the linked list .
No modification allowed Linked list .
Title Example :

This question and The last question is basically the same , The only difference is that we need to return the first node of the linked list to enter the ring .
The key to this problem is to find which node next Was visited , Once found , We directly return that will do .
public ListNode detectCycle(ListNode head) {
if(head==null||head.next==null)return null;
List<ListNode>list=new ArrayList<>();
while(!list.contains(head)&&head!=null){
list.add(head);
if(list.contains(head.next)){
return head.next;
}
head=head.next;
}
return null;
}
边栏推荐
- HCIP实验
- 数字签名技术简介
- Hcip day 8 notes
- IP address and subnet division (A2)
- 使用C语言和libcyaml库解析yaml配置文件
- "Guanghetong AI intelligent module sca825-w" with full AI performance accelerates the era of e-commerce live broadcast 2.0
- OSPF (sixth day notes)
- Research on retinal vascular segmentation based on GAN using few samples
- HCIP第十天笔记
- HCIP中的MGRE GRE OSPF过程
猜你喜欢
随机推荐
HCIP第四天笔记
Disadvantages of win11
Rip --- routing information protocol
Hardware knowledge 2 -- Protocol class (based on Baiwen hardware operation Daquan video tutorial)
Hcip third day notes
Hcip second day notes
128. 最长连续序列
HCIP第三天笔记
HCIA的复习
HCIP第七天笔记
"Guanghetong AI intelligent module sca825-w" with full AI performance accelerates the era of e-commerce live broadcast 2.0
Hcip day 9 notes
HCIP,OSPF综合实验
Arm architecture and programming 2 -- arm architecture (based on Baiwen arm architecture and programming tutorial video)
Summary of HCIA knowledge points
数字化转型时代的企业数据新基建 | 爱分析报告
Customer first | domestic Bi leader, smart software completes round C financing
Parsing yaml configuration files using C language and libcyaml Library
Matlab绘制双坐标图(全网最简单)
HCIP第一天笔记









