当前位置:网站首页>[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;
}
}
};
边栏推荐
- vim命令行笔记
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- Symbolic representation of functions in deep learning papers
- GCC compilation options
- Walk into WPF's drawing Bing Dwen Dwen
- Arduino JSON data information parsing
- STM32 how to locate the code segment that causes hard fault
- MySQL time, time zone, auto fill 0
- Arduino gets the length of the array
猜你喜欢
![Detailed explanation of Union [C language]](/img/d2/99f288b1705a3d072387cd2dde827c.jpg)
Detailed explanation of Union [C language]

ARM PC=PC+8 最便于理解的阐述

单片机蓝牙无线烧录

dosbox第一次使用

Basic operations of databases and tables ----- classification of data

(五)R语言入门生物信息学——ORF和序列分析

Générateur d'identification distribué basé sur redis

MySQL占用内存过大解决方案

Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports

基於Redis的分布式ID生成器
随机推荐
Keyword inline (inline function) usage analysis [C language]
Common properties of location
. elf . map . list . Hex file
Programmers can make mistakes. Basic pointers and arrays of C language
Cannot change version of project facet Dynamic Web Module to 2.3.
嵌入式启动流程
Dead loop in FreeRTOS task function
Imgcat usage experience
1081 rational sum (20 points) points add up to total points
(五)R语言入门生物信息学——ORF和序列分析
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
Priority inversion and deadlock
@Autowired 和 @Resource 的区别
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
[Red Treasure Book Notes simplified version] Chapter 12 BOM
@The difference between Autowired and @resource
基于Redis的分布式锁 以及 超详细的改进思路