当前位置:网站首页>LeetCode链表问题——142.环形链表II(一题一文学会链表)
LeetCode链表问题——142.环形链表II(一题一文学会链表)
2022-07-28 19:46:00 【十八岁讨厌Java】
一、题目描述:
难度中等1688收藏分享切换为英文接收动态反馈
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。

二、代码
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}三、了解链表
什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)。链接的入口节点称为链表的头结点也就是head。
单链表:
双链表:
每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
双链表 既可以向前查询也可以向后查询。

循环链表:
循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。

链表的存储方式:
数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
所以链表中的节点在内存中不是连续分布的 ,而是散乱分布在内存中的某地址上,分配机制取决于操作系统的内存管理。
删除节点:
只要将C节点的next指针 指向E节点就可以了。

添加节点:

与数组对比:

边栏推荐
- Another installation artifact
- 编码用这16个命名规则能让你少写一半以上的注释!
- Cobal Strike的学习与使用
- 八、QOS队列调度与报文丢弃
- C process control statement
- Invalid prompt object name in SQL Server
- How Oracle exports data (how Oracle backs up databases)
- Jiuxin intelligence officially joined opengauss community
- ABB electromagnetic flowmeter maintenance signal transmitter maintenance 41f/e4 technical parameters
- Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
猜你喜欢

protobuf 中基础数据类型的读写

详细讲解C语言12(C语言系列)

Redis缓存雪崩、缓存穿透、缓存击穿

Icml2022 | timing self-monitoring video transformer

Several skills of API interface optimization

CVPR 2022 | 网络中批处理归一化估计偏移的深入研究

Kubedm builds kubernetes cluster

八、QOS队列调度与报文丢弃
![[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)](/img/f0/7e3ea94e02a42b6055c40b58d1e39c.png)
[input ID number] is replaced by an asterisk, and input is cut into multiple small squares (similar)

CVPR 2022 | in depth study of batch normalized estimation offset in network
随机推荐
Uncaught Error:Invalid geoJson format Cannot read property ‘length‘ of undefind
1162. Map analysis - non recursive method
Pytorch学习记录(三):随机梯度下降、神经网络与全连接
Maintenance of delta hot metal detector principle analysis of v5g-jc-r1 laser measurement sensor / detector
[tidb] importing TXT documents into the database is really efficient
Go concurrent programming basics
Confession of a graduate student: why am I addicted to opengauss community?
微服务架构下的系统集成
MySQL sorts out the review content -- with mind map
C # detailed steps for connecting to MySQL database
苹果M1处理器详解:性能及能效成倍提升,Intel酷睿i9也不是对手!
Study and use of cobalt strike
ctfshow 做题 web模块 web11~web14
关于一些小需求,用案例方式记录
What is ci/cd| Achieve faster and better software delivery
Timing analysis and constraints based on Xilinx
The ref value ‘xxx‘ will likely have changed by the time this effect function runs.If this ref......
ICML2022 | 时序自监督视频transformer
Applet container technology improves mobile R & D efficiency by 500%
BUUCTF做题Upload-Labs记录pass-01~pass-10