当前位置:网站首页>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 ( becausehead
Move backward , The value in front of the linked list is lost ) dummy.next
Point 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;
}
};
边栏推荐
- Apache middleware vulnerability recurrence
- Uibehavior, a comprehensive exploration of ugui source code
- 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
- PHP - Common magic method (nanny level teaching)
- Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
- Parameter self-tuning of relay feedback PID controller
- Vit (vision transformer) principle and code elaboration
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- Google可能在春节后回归中国市场。
- 珠海金山面试复盘
猜你喜欢
Database basic commands
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
解决方案:智慧工地智能巡檢方案視頻監控系統
珠海金山面试复盘
21. Delete data
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
Relevant introduction of clip image
Simulation of Teman green interferometer based on MATLAB
MEX有关的学习
随机推荐
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
On why we should program for all
Description of octomap averagenodecolor function
数据治理:误区梳理篇
edge浏览器 路径获得
. Net 6 learning notes: what is NET Core
Brief explanation of instagram operation tips in 2022
继电反馈PID控制器参数自整定
Key value judgment in the cycle of TS type gymnastics, as keyword use
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
Redis list detailed explanation of character types yyds dry goods inventory
数据治理:主数据的3特征、4超越和3二八原则
Interview Reply of Zhuhai Jinshan
[count] [combined number] value series
Solution: intelligent site intelligent inspection scheme video monitoring system
49. Sound card driven article collection
[untitled]
Three treasures of leeks and Chinese men's football team
In the era of digital economy, how to ensure security?
P3047 [usaco12feb]nearby cows g (tree DP)