当前位置:网站首页>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;
}
};
边栏推荐
- TS类型体操 之 字符串的妙用
- Typescript interface and the use of generics
- [Yugong series] creation of 009 unity object of U3D full stack class in February 2022
- HTTP cache, forced cache, negotiated cache
- [t31zl intelligent video application processor data]
- 21. Delete data
- Oracle time display adjustment
- Leecode-c language implementation -15 Sum of three ----- ideas to be improved
- 【Redis】NoSQL数据库和redis简介
- Opencv learning notes 8 -- answer sheet recognition
猜你喜欢
opencv学习笔记八--答题卡识别
Opencv learning notes 9 -- background modeling + optical flow estimation
QT color is converted to string and uint
Google may return to the Chinese market after the Spring Festival.
数字经济时代,如何保障安全?
Linked list interview questions (Graphic explanation)
【T31ZL智能视频应用处理器资料】
Apache middleware vulnerability recurrence
Uibehavior, a comprehensive exploration of ugui source code
链表面试题(图文详解)
随机推荐
Epoll and IO multiplexing of redis
Opencv learning notes 8 -- answer sheet recognition
JMeter performance test steps practical tutorial
Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
Qualitative risk analysis of Oracle project management system
[dictionary tree] [trie] p3879 [tjoi2010] reading comprehension
What are the ways to download network pictures with PHP
MEX有关的学习
解决方案:智慧工地智能巡检方案视频监控系统
Methods for JS object to obtain attributes (. And [] methods)
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
[1. Delphi foundation] 1 Introduction to Delphi Programming
二叉树创建 & 遍历
PHP Coding Standard
QT color is converted to string and uint
Transformer principle and code elaboration
Relevant introduction of clip image
opencv学习笔记八--答题卡识别
Is the super browser a fingerprint browser? How to choose a good super browser?
Position() function in XPath uses