当前位置:网站首页>Leetcode daily practice - 203. remove linked list elements
Leetcode daily practice - 203. remove linked list elements
2022-07-27 21:06:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 203. Remove linked list elements
Let’s get it!

List of articles
1. Topic analysis
Give you a list of the head node head And an integer val , Please delete all the contents in the linked list
Node.val == valThe node of , And back to New head node .
Example 1:
Example 2:
Example 3:
2. Title diagram
The problem is very simple , Or adopt Double pointer Law , But there are several situations to consider
(1) Routine situation
(2) There are continuous in the linked list val
(3) The head node is val
Routine situation
Define a prev Pointer to NULL; Let me define one more cur The pointer points to the head node 
The first 1 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 2 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 3 Step : here cur The element that points to is equal to 6, So the element 6 Set as NULL, And then let prev Of next Point to elements 3,cur Of next It also points to elements 3 ( As shown in the figure )
The first 4 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 5 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 6 Step : here cur The element that points to is equal to 6, So the element 6 Set as NULL, And then let prev Of next Point to cur Of next( As shown in the figure )
The first 7 Step : When cur The element pointed to is empty when , The loop ends ( As shown in the figure )
Dynamic diagram demonstration 
continuity val situation
Or to define a prev Pointer to NULL; Let me define one more cur The pointer points to the head node ( As shown in the figure ).
The first 1 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 2 Step :cur The element pointed to is not equal to 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 3 Step : here cur The element that points to is equal to 6, Then delete the element , And then let prev Of next Point to cur Of next,cur Point to the next element ( As shown in the figure )
The first 4 Step : here cur The element pointed to is still equal to 6, Then delete the element , And then let prev Of next Point to cur Of next,cur Point to the next element ( As shown in the figure )
The first 5 Step : here cur The element pointed to is still equal to 6, Then delete the element , And then let prev Of next Point to cur Of next,cur Point to the next element ( As shown in the figure )
The first 6 Step : here cur The element pointed to is not 6, that prev and cur Move backward in turn ( As shown in the figure )
The first 7 Step : here cur The element that points to is equal to 6, Then delete the element and let prev Of next Point to cur Of next,cur Point to empty , End cycle ( As shown in the figure )
You can see , If there are continuous val, Then the practice is the same as the conventional situation .
The head node is val
Or to define a prev Pointer to NULL; Let me define one more cur The pointer points to the head node ( As shown in the figure ).
At this point, a problem can be found , If it is calculated according to the normal situation, it is ,cur The element that points to is equal to 6, Then delete the element , And then let prev Of next Point to cur Of next,cur Point to the next element .
But at this time prev The element pointed to is empty , cur Of next How can I assign a value to empty Well ? Obviously not , We have to find another way !
It's very simple , Let's define one more curHead The pointer points to the head node ( As shown in the figure ).
Here, a dynamic diagram is directly used to demonstrate the whole process 
3. Code implementation
Interface code
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* prev, *cur;
prev = NULL;
cur = head;
// The end condition of the loop is cur It's empty
while (cur) {
// cur It's not equal to val
if (cur->val != val) {
prev = cur; // Put... Directly cur Address assigned to prev, Is equivalent to prev Move to cur Location
cur = cur->next; // cur Also move one position backwards
}
else {
// When cur be equal to val
struct ListNode* curHead = cur->next;
// Why? curHead The content is cur->next, instead of cur Well ?
// Suppose that the head node is equal to val, Then delete val in the future , It is equivalent to header deletion
// After deleting the header , If curHead Yes, the content is cur If you have your address , I can't find the following list for so long
// therefore curHead The deposit should be cur->next, That is, the address of the second node
if (prev == NULL) {
// The head node is val by val The situation of
free(cur); // release cur
head = curHead; // Update the header node
cur = curHead; // Then assign the address of the new head node to cur
}
else {
// The header node is not val by val The situation of
free(cur); // Release val Elements
prev->next = curHead; // because curHead Deposit is cur Of next, That is to say cur The address of the next node , So it is directly assigned to prev Of next
cur = curHead; // And then let cur Point to the address of the next node
}
}
}
return head;
}
Submit results 
边栏推荐
- Hexagon_V65_Programmers_Reference_Manual(9)
- User login switching case
- 程序放在哪儿?
- [deep learning] pytoch torch Autograd automatic differential engine
- 中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
- IOU 目标跟踪其一:IOU Tracker
- 自动化测试----selenium(二)
- R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用deviance函数计算出模型的残差平方和
- Technology blog and tutorial
- 搭建discuz论坛并攻破盗取数据库
猜你喜欢
![Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]](/img/ed/6f4da22e86b44935fc84e3b4901c48.png)
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]

VI working mode (3 kinds) and mode switching (conversion)

基于文件上传漏洞获得网站 shell 权限

sql编码bug

LeetCode每日一练 —— CM11 链表分割

One article to understand pychar shortcut key
![[dart] a programming language for cross end development](/img/e1/1167a322bb9f276f2e00fb12414d17.png)
[dart] a programming language for cross end development

Read Plato & nbsp; Eplato of farm and the reasons for its high premium

How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help

Arduino development (II)_ RGB light control method based on Arduino uno development board
随机推荐
Qt opengl 让物体在关照下动起来,形成动画
js闭包知识
Sscanf caused the address to be out of bounds
Beijing / Shanghai / Guangzhou / Shenzhen dama-cdga/cdgp data governance certification registration conditions
SQL coding bug
[dart] a programming language for cross end development
Installation and use tutorial of the latest version of Web vulnerability scanning tool appscan\awvs\xray
How to calculate the execution time in the function resource usage when using the timer trigger type to process database data?
LeetCode-136-只出现一次的数字
Brand list cases
Technology blog and tutorial
CPDA | how to have data analysis thinking?
Leetcode-136-a number that appears only once
14天鸿蒙设备开发实战-第七章 设备联网上云 学习笔记
Hexagon_V65_Programmers_Reference_Manual(6)
How to talk to CIO / CTO
How to solve the problem that tp6 controller does not exist: app\controller\index
Typoa spelling check: missing dictionary file for Chinese
R语言使用dplyr包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组均值(mean)并合并生成的统计数据到原数据集中
LeetCode-209-长度最小的子数组