当前位置:网站首页>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;
}
};
边栏推荐
- 数字经济时代,如何保障安全?
- Common functions for PHP to process strings
- 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
- MEX有关的学习
- How to prevent Association in cross-border e-commerce multi account operations?
- Onie supports pice hard disk
- Basics of reptile - Scratch reptile
- Parameter self-tuning of relay feedback PID controller
- [CF Gym101196-I] Waif Until Dark 网络最大流
- C intercept string
猜你喜欢

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

Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
![[1. Delphi foundation] 1 Introduction to Delphi Programming](/img/14/272f7b537eedb0267a795dba78020d.jpg)
[1. Delphi foundation] 1 Introduction to Delphi Programming
![DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist](/img/17/415e66d67afb055e94a966de25c2bc.png)
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist

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

Relevant introduction of clip image

Sanzi chess (C language)

Document 2 Feb 12 16:54
![[count] [combined number] value series](/img/f5/6fadb8f1c8b75ddf5994c2c43feaa6.jpg)
[count] [combined number] value series

Description of octomap averagenodecolor function
随机推荐
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
edge浏览器 路径获得
C # connect to SQLite database to read content
A Closer Look at How Fine-tuning Changes BERT
edge瀏覽器 路徑獲得
【Redis】NoSQL数据库和redis简介
【T31ZL智能视频应用处理器资料】
Typescript interface and the use of generics
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
Solution: intelligent site intelligent inspection scheme video monitoring system
[非线性控制理论]9_非线性控制理论串讲
[t31zl intelligent video application processor data]
解决方案:智慧工地智能巡檢方案視頻監控系統
智能终端设备加密防护的意义和措施
Cf1036c class numbers solution
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
[Yugong series] February 2022 U3D full stack class 010 prefabricated parts
. Net 6 learning notes: what is NET Core
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
Oracle time display adjustment