当前位置:网站首页>[offer18] delete the node of the linked list
[offer18] delete the node of the linked list
2022-07-06 12:24:00 【Vigorous waist Nuo dance】
problem
Given the head pointer of one-way linked list and the value of a node to be deleted , Define a function to delete the node .
Return the head node of the deleted linked list .
Be careful : This question is different from the original one
Example 1:
Input : head = [4,5,1,9], val = 5
Output : [4,1,9]
explain : Given that the value of your list is 5 Second node of , So after calling your function , The list should be 4 -> 1 -> 9.
Example 2:
Input : head = [4,5,1,9], val = 1
Output : [4,5,9]
explain : Given that the value of your list is 1 The third node of , So after calling your function , The list should be 4 -> 5 -> 9.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/shan-chu-lian-biao-de-jie-dian-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class Solution {
public:
/* Return the head pointer of the linked list , Or use the header node ->next Realization , Or dynamically maintain the header pointer . The code selects the second */
ListNode* deleteNode(ListNode* head, int val) {
/* Traverse a pointer of the linked list */
ListNode* current = head;
/* The essence of deleting a node is to find its precursor .*/
ListNode* prior = head;
/* The length of the linked list is not specified . Then keep cycling .*/
while (1) {
/* When the node is found */
if (current->val == val) {
/* Come up and directly find the target , The first node has no precursor . Require special treatment */
if (current == head) {
/* First consciousness is actually head++, Abandoned */
head = head->next;
return head;
}
else {
prior->next = current->next;
return head;
}
}
/* If not found , Back up the current node */
prior = current;
current = current->next;
}
}
};
边栏推荐
- open-mmlab labelImg mmdetection
- 关于Gateway中使用@Controller的问题
- Arduino gets the length of the array
- VSCode基础配置
- Programmers can make mistakes. Basic pointers and arrays of C language
- Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
- RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
- JS变量类型以及常用类型转换
- 程序员老鸟都会搞错的问题 C语言基础 指针和数组
- 2021.11.10汇编考试
猜你喜欢

AMBA、AHB、APB、AXI的理解

Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
![Several declarations about pointers [C language]](/img/9b/ace0abbd1956123a945a98680b1e86.png)
Several declarations about pointers [C language]

JS function promotion and declaration promotion of VaR variable

(1) Introduction Guide to R language - the first step of data analysis

Arm pc=pc+8 is the most understandable explanation

Arduino JSON data information parsing

VSCode基础配置

JS正则表达式基础知识学习

JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
随机推荐
Minio file download problem - inputstream:closed
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Arduino get random number
A possible cause and solution of "stuck" main thread of RT thread
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Several declarations about pointers [C language]
MySQL时间、时区、自动填充0的问题
[Offer18]删除链表的节点
About using @controller in gateway
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
JS regular expression basic knowledge learning
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
JS variable types and common type conversions
C language, log print file name, function name, line number, date and time
Basic operations of databases and tables ----- classification of data
RT thread API reference manual
Common properties of location
Kconfig Kbuild
Knowledge summary of request
By v$rman_ backup_ job_ Oracle "bug" caused by details