当前位置:网站首页>Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
Leetcode linked list problem - 203. remove the linked list elements (learn the linked list by one question and one article)
2022-07-26 05:10:00 【18-year-old hates Java】
One 、 Title Description :
203. Remove linked list elements
Give you a list of the head node head And an integer val , Please delete all the contents in the linked list Node.val == val The node of , And back to New head node .

Two 、 Code
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return head;
}
// Because the deletion may involve the head node , So set dummy node , Unified operation
ListNode dummy = new ListNode(-1, head);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return dummy.next;
}3、 ... and 、 Understand linked list
What is a linked list , A linked list is a linear structure connected in series by pointers , Each node consists of two parts , One is the data field and the other is the pointer field ( Holds a pointer to the next node ), The pointer field of the last node points to null( A null pointer means ). The entry node of the link is called the head node of the linked list head.
Single chain list :
Double linked list :
Each node has two pointer fields , One points to the next node , One points to the previous node .
Double linked list You can query forward or backward .

Circular linked list :
Circular linked list , seeing the name of a thing one thinks of its function , The linked list is connected end to end . Circular list can be used to solve the Joseph Ring problem .

How to store linked lists :
Arrays are continuously distributed in memory , But linked lists are not continuously distributed in memory .
The linked list links the nodes in the memory through the pointer of the pointer field .
So the nodes in the linked list are not continuously distributed in memory , It's scattered on some address in memory , The allocation mechanism depends on the memory management of the operating system .
Delete node :
As long as C Node next The pointer Point to E Node is OK .

Add a node :

Compare with array :

边栏推荐
- Your understanding of the "happen before principle" may be wrong?
- Security permission management details
- Nacos 介绍和部署
- JVM第六讲:线上环境 FGC 频繁,如何解决?
- 基于遥感解译与GIS技术环境影响评价图件制作
- Add and modify the verification logic, and use -validation- to complete the group verification
- mysql函数汇总之日期和时间函数
- 嵌入式分享合集21
- 提高shuffle操作中的reduce并行度
- 阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
猜你喜欢

An online accident, I suddenly realized the essence of asynchrony

JVM第五讲:纵横数据如何应对洪峰推送

面试之请详细说下synchronized的实现原理以及相关的锁

ALV程序收集

Axi protocol (4): signals on the Axi channel

Mysql主从同步及主从同步延迟解决方案

未来大气污染变化模拟

Shell的read 读取控制台输入、read的使用

Embedded sharing collection 21

When AQS wakes up the thread, I understand why it traverses from the back to the front
随机推荐
关于负数表示的数值为什么比整数大1?
没背景、没学历?专科测试员进入互联网大厂是不是真的没希望?
Date and time function of MySQL function summary
遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
Recommendation system - machine learning
Practical technology of SWAT Model in simulation of hydrology, water resources and non-point source pollution
JVM Lecture 5: how to deal with peak push of vertical and horizontal data
异步时父子线程间的ThreadLocal传递问题
Molecular skeleton transition tool -delinker introduction
Ggjj, do you have a look at this problem? Does caching cause cross domain problems?
AQS唤醒线程的时候为什么从后向前遍历,我懂了
A material of machine learning
地球系统模式(CESM)实践技术
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
How to reproduce the official course of yolov5 gracefully (II) -- Mark and train your own data set
Teach you how to use code to realize SSO single sign on
Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disasters, ecology, environment and health
Interprocess communication
MODFLOW flex, GMS, FEFLOW, hydraus practical application
嵌入式分享合集21