当前位置:网站首页>[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;
}
}
};
边栏推荐
- Get the position of the nth occurrence of the string
- Selective sorting and bubble sorting [C language]
- Working principle of genius telephone watch Z3
- How to add music playback function to Arduino project
- Redis based distributed locks and ultra detailed improvement ideas
- Vscode basic configuration
- JUC forkjoin and completable future
- Dead loop in FreeRTOS task function
- 單片機藍牙無線燒錄
- AMBA、AHB、APB、AXI的理解
猜你喜欢
Générateur d'identification distribué basé sur redis
NRF24L01故障排查
Cannot change version of project facet Dynamic Web Module to 2.3.
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
The dolphin scheduler remotely executes shell scripts through the expect command
程序员老鸟都会搞错的问题 C语言基础 指针和数组
基于Redis的分布式锁 以及 超详细的改进思路
Kconfig Kbuild
[Red Treasure Book Notes simplified version] Chapter 12 BOM
JS variable types and common type conversions
随机推荐
Pytorch: tensor operation (I) contiguous
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Basic operations of databases and tables ----- view data tables
Cannot change version of project facet Dynamic Web Module to 2.3.
ES6 grammar summary -- Part I (basic)
Talking about the startup of Oracle Database
NRF24L01 troubleshooting
Whistle+switchyomega configure web proxy
Common DOS commands
Redis cache update strategy, cache penetration, avalanche, breakdown problems
STM32 how to locate the code segment that causes hard fault
程序设计大作业:教务管理系统(C语言)
open-mmlab labelImg mmdetection
NRF24L01故障排查
[Leetcode15]三数之和
MySQL時間、時區、自動填充0的問題
程序员老鸟都会搞错的问题 C语言基础 指针和数组
level16
About using @controller in gateway
Conditional probability