当前位置:网站首页>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;
}
};
边栏推荐
- Google可能在春节后回归中国市场。
- 实现精细化生产, MES、APS、ERP必不可少
- Codeforces Global Round 19(A~D)
- C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
- . Net 6 learning notes: what is NET Core
- 21. Delete data
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- onie支持pice硬盘
- Luogu p4127 [ahoi2009] similar distribution problem solution
- 24. Query table data (basic)
猜你喜欢

A Closer Look at How Fine-tuning Changes BERT

Opencv learning notes 9 -- background modeling + optical flow estimation

leecode-C語言實現-15. 三數之和------思路待改進版
![[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems](/img/6d/282d904810807810adb06b071fb39b.jpg)
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems

智能终端设备加密防护的意义和措施

MEX有关的学习
![[cf gym101196-i] waif until dark network maximum flow](/img/66/6b339fc23146b5fbdcd2a1fa0a2349.png)
[cf gym101196-i] waif until dark network maximum flow

ROS learning (IX): referencing custom message types in header files

Games101 Lesson 7 shading 1 Notes

Qualitative risk analysis of Oracle project management system
随机推荐
MEX有关的学习
数字经济时代,如何保障安全?
Yu Xia looks at win system kernel -- message mechanism
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
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
[Yugong series] February 2022 U3D full stack class 010 prefabricated parts
[1. Delphi foundation] 1 Introduction to Delphi Programming
Luogu p1836 number page solution
08- [istio] istio gateway, virtual service and the relationship between them
Pangolin Library: control panel, control components, shortcut key settings
Nc204382 medium sequence
C # create database connection object SQLite database
[非线性控制理论]9_非线性控制理论串讲
Transformer principle and code elaboration
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
. Net 6 learning notes: what is NET Core
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
Le chemin du navigateur Edge obtient
P3047 [USACO12FEB]Nearby Cows G(树形dp)