当前位置:网站首页>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;
}
};
边栏推荐
- 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
- Solution: intelligent site intelligent inspection scheme video monitoring system
- TS类型体操 之 字符串的妙用
- Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
- edge浏览器 路径获得
- 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
- 数据治理:误区梳理篇
- 解决方案:智慧工地智能巡检方案视频监控系统
- How to prevent Association in cross-border e-commerce multi account operations?
- 上线APS系统,破除物料采购计划与生产实际脱钩的难题
猜你喜欢
File upload of DVWA range
leecode-C语言实现-15. 三数之和------思路待改进版
Google may return to the Chinese market after the Spring Festival.
Opencv learning notes 9 -- background modeling + optical flow estimation
【T31ZL智能视频应用处理器资料】
[CF Gym101196-I] Waif Until Dark 网络最大流
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
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
珠海金山面试复盘
22. Empty the table
随机推荐
Position() function in XPath uses
Three treasures of leeks and Chinese men's football team
leecode-C语言实现-15. 三数之和------思路待改进版
MEX有关的学习
edge瀏覽器 路徑獲得
2.10transfrom attribute
Solution: intelligent site intelligent inspection scheme video monitoring system
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
Significance and measures of encryption protection for intelligent terminal equipment
C # create database connection object SQLite database
MES, APS and ERP are essential to realize fine production
Data governance: Data Governance under microservice architecture
链表面试题(图文详解)
Data governance: data quality
Sharing of source code anti disclosure scheme under burning scenario
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Document 2 Feb 12 16:54
WebRTC系列-H.264预估码率计算
Data governance: metadata management
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network