当前位置:网站首页>[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;
}
}
};
边栏推荐
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Variable parameter principle of C language function: VA_ start、va_ Arg and VA_ end
- [golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
- [esp32 learning-1] construction of Arduino esp32 development environment
- gcc 编译选项
- ESP学习问题记录
- MySQL time, time zone, auto fill 0
- Walk into WPF's drawing Bing Dwen Dwen
- Basic operations of databases and tables ----- classification of data
- Redis cache update strategy, cache penetration, avalanche, breakdown problems
猜你喜欢
JS正则表达式基础知识学习
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Types de variables JS et transformations de type communes
JS数组常用方法的分类、理解和运用
Arduino JSON data information parsing
Basic operations of databases and tables ----- classification of data
数据库课程设计:高校教务管理系统(含代码)
level16
CUDA C programming authoritative guide Grossman Chapter 4 global memory
MySQL占用内存过大解决方案
随机推荐
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
NRF24L01故障排查
记一次云服务器被密码爆破的经历——关小黑屋、改密码、改端口
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
(1) Introduction Guide to R language - the first step of data analysis
Vscode basic configuration
Single chip Bluetooth wireless burning
Arduino JSON data information parsing
[offer9]用两个栈实现队列
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
Arduino get random number
(一)R语言入门指南——数据分析的第一步
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
ESP8266连接onenet(旧版MQTT方式)
Common DOS commands
ORA-02030: can only select from fixed tables/views
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
Classification, understanding and application of common methods of JS array
Générateur d'identification distribué basé sur redis
ES6语法总结--上篇(基础篇)