当前位置:网站首页>[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;
}
}
};
边栏推荐
- js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- [Nodejs] 20. Koa2 onion ring model ----- code demonstration
- Redis based distributed locks and ultra detailed improvement ideas
- 如何给Arduino项目添加音乐播放功能
- Common properties of location
- How to add music playback function to Arduino project
- open-mmlab labelImg mmdetection
- The dolphin scheduler remotely executes shell scripts through the expect command
- [esp32 learning-2] esp32 address mapping
- Arduino get random number
猜你喜欢
Basic operations of databases and tables ----- view data tables
Programmers can make mistakes. Basic pointers and arrays of C language
RT thread API reference manual
[esp32 learning-2] esp32 address mapping
Time slice polling scheduling of RT thread threads
ES6 grammar summary -- Part I (basic)
Priority inversion and deadlock
Symbolic representation of functions in deep learning papers
JS变量类型以及常用类型转换
Several declarations about pointers [C language]
随机推荐
ESP学习问题记录
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
ORA-02030: can only select from fixed tables/views
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
1081 rational sum (20 points) points add up to total points
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
Single chip Bluetooth wireless burning
JS正则表达式基础知识学习
History object
Mysqldump error1066 error solution
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Pat 1097 duplication on a linked list (25 points)
Important methods of array and string
Programmers can make mistakes. Basic pointers and arrays of C language
vim命令行笔记
[esp32 learning-1] construction of Arduino esp32 development environment
STM32 how to locate the code segment that causes hard fault
[offer9]用两个栈实现队列
level16