当前位置:网站首页>Leetcode question brushing record | 203_ Remove linked list elements
Leetcode question brushing record | 203_ Remove linked list elements
2022-07-06 07:53:00 【coder_ sure】
leetcode Record of writing questions |203 _ Remove linked list elements
author github link : github link
Force to buckle 203 topic
type : Linked list
subject :
Give you a list of the head node head And an integer val , Please delete all the contents in the linked list Node.val == val The node of , And back to New head node .
Example 1

Input :head = [1,2,6,3,4,5,6], val = 6
Output :[1,2,3,4,5]
Example 2
Input :head = [], val = 1
Output :[]
Example 3
Input :head = [7,7,7,7], val = 7
Output :[]
Their thinking
Train of thought reminder : Three nodes are used to traverse
Train of thought details :
- Define a temporary node
dummy, Put it at the beginning of the whole linked list ( becauseheadMove backward , The value in front of the linked list is lost ) dummy.nextPoint tohead- Define another variable
prev, Responsible for following head The last linked list to be generated is reserved one by one
- Until the traversal is complete , return dummy.next
python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
if head is None :
return None
dummy = ListNode(0)
dummy.next = head
prev = dummy
while(head!=None):
if head.val == val:
prev.next = head.next
head=head.next
else:
prev = head
head = head.next
return dummy.next
c++
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(!head) return NULL;
ListNode * dummy = new ListNode(0);
dummy -> next = head;
ListNode * prev=dummy;
while(head!=NULL){
if(head->val==val){
prev->next = head->next;
head = head->next;
}
else{
prev = head;
head = head->next;
}
}
return dummy->next;
}
};
边栏推荐
- [Yugong series] creation of 009 unity object of U3D full stack class in February 2022
- 数据治理:元数据管理篇
- octomap averageNodeColor函数说明
- Secure captcha (unsafe verification code) of DVWA range
- esRally国内安装使用避坑指南-全网最新
- 软件开发的一点随记
- PHP - Common magic method (nanny level teaching)
- Games101 Lesson 7 shading 1 Notes
- Get the path of edge browser
- Iterator Foundation
猜你喜欢

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

数字经济时代,如何保障安全?

【T31ZL智能视频应用处理器资料】

Pangolin Library: control panel, control components, shortcut key settings

Related operations of Excel
![[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map](/img/c3/1b6013bfb2441219bf621c3f0726ea.jpg)
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
![[untitled]](/img/38/bc025310b9742b5bf0bd28c586ec0d.jpg)
[untitled]

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

leecode-C語言實現-15. 三數之和------思路待改進版

esRally国内安装使用避坑指南-全网最新
随机推荐
Opencv learning notes 8 -- answer sheet recognition
24. Query table data (basic)
洛谷P1836 数页码 题解
xpath中的position()函数使用
Data governance: misunderstanding sorting
Key value judgment in the cycle of TS type gymnastics, as keyword use
[nonlinear control theory]9_ A series of lectures on nonlinear control theory
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
21. Delete data
Typescript interface and the use of generics
A Closer Look at How Fine-tuning Changes BERT
WebRTC系列-H.264预估码率计算
珠海金山面试复盘
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Solution: intelligent site intelligent inspection scheme video monitoring system
ROS learning (IX): referencing custom message types in header files
MySQL view tablespace and create table statements
数据治理:元数据管理篇
P3047 [usaco12feb]nearby cows g (tree DP)
Relevant introduction of clip image