当前位置:网站首页>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;
}
};
边栏推荐
- [CF Gym101196-I] Waif Until Dark 网络最大流
- Flash return file download
- shu mei pai
- [Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
- Linked list interview questions (Graphic explanation)
- TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
- Three treasures of leeks and Chinese men's football team
- TS 类型体操 之 循环中的键值判断,as 关键字使用
- Methods for JS object to obtain attributes (. And [] methods)
- Type of data in energy dashboard
猜你喜欢
[computer skills]
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
Risk planning and identification of Oracle project management system
Opencv learning notes 8 -- answer sheet recognition
21. Delete data
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
Linked list interview questions (Graphic explanation)
Description of octomap averagenodecolor function
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
随机推荐
In the era of digital economy, how to ensure security?
shu mei pai
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
Binary tree creation & traversal
Notes on software development
21. Delete data
Related operations of Excel
[cf gym101196-i] waif until dark network maximum flow
22. Empty the table
Flash return file download
Methods for JS object to obtain attributes (. And [] methods)
ROS learning (IX): referencing custom message types in header files
Google可能在春节后回归中国市场。
Data governance: data quality
Sharing of source code anti disclosure scheme under burning scenario
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
Key value judgment in the cycle of TS type gymnastics, as keyword use
洛谷P4127 [AHOI2009]同类分布 题解
链表面试题(图文详解)