当前位置:网站首页>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;
}
};
边栏推荐
- octomap averageNodeColor函数说明
- [非线性控制理论]9_非线性控制理论串讲
- Data governance: metadata management
- Binary tree creation & traversal
- MySQL view tablespace and create table statements
- Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
- Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
- Wonderful use of TS type gymnastics string
- What are the ways to download network pictures with PHP
- Cf1036c class numbers solution
猜你喜欢

Relevant introduction of clip image
![[1. Delphi foundation] 1 Introduction to Delphi Programming](/img/14/272f7b537eedb0267a795dba78020d.jpg)
[1. Delphi foundation] 1 Introduction to Delphi Programming

Related operations of Excel
![[count] [combined number] value series](/img/f5/6fadb8f1c8b75ddf5994c2c43feaa6.jpg)
[count] [combined number] value series
![[redis] Introduction to NoSQL database and redis](/img/95/229d7a08e94245f2733b8c59201cff.png)
[redis] Introduction to NoSQL database and redis

Document 2 Feb 12 16:54

A Closer Look at How Fine-tuning Changes BERT

Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed

Pangolin Library: control panel, control components, shortcut key settings
![[nonlinear control theory]9_ A series of lectures on nonlinear control theory](/img/a8/03ed363659a0a067c2f1934457c106.png)
[nonlinear control theory]9_ A series of lectures on nonlinear control theory
随机推荐
数据治理:元数据管理篇
Luogu p1836 number page solution
二叉树创建 & 遍历
JMeter performance test steps practical tutorial
Uibehavior, a comprehensive exploration of ugui source code
Games101 Lesson 7 shading 1 Notes
How to prevent Association in cross-border e-commerce multi account operations?
洛谷P1836 数页码 题解
数据治理:微服务架构下的数据治理
MES, APS and ERP are essential to realize fine production
861. Score after flipping the matrix
Three treasures of leeks and Chinese men's football team
649. Dota2 Senate
flask返回文件下载
[nonlinear control theory]9_ A series of lectures on nonlinear control theory
07- [istio] istio destinationrule (purpose rule)
Webrtc series-h.264 estimated bit rate calculation
洛谷P4127 [AHOI2009]同类分布 题解
[cf gym101196-i] waif until dark network maximum flow
数字经济时代,如何保障安全?