当前位置:网站首页>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 体操 &(交叉运算) 和 接口的继承的区别
- octomap averageNodeColor函数说明
- 洛谷P1836 数页码 题解
- . Net 6 learning notes: what is NET Core
- 数据治理:误区梳理篇
- 解决方案:智慧工地智能巡检方案视频监控系统
- Wonderful use of TS type gymnastics string
- Three treasures of leeks and Chinese men's football team
- MES, APS and ERP are essential to realize fine production
- leecode-C语言实现-15. 三数之和------思路待改进版
猜你喜欢
Google可能在春节后回归中国市场。
Apache middleware vulnerability recurrence
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
opencv学习笔记八--答题卡识别
[redis] Introduction to NoSQL database and redis
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
esRally国内安装使用避坑指南-全网最新
24. Query table data (basic)
数据治理:主数据的3特征、4超越和3二八原则
随机推荐
Get the path of edge browser
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
[nonlinear control theory]9_ A series of lectures on nonlinear control theory
[computer skills]
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
esRally国内安装使用避坑指南-全网最新
WebRTC系列-H.264预估码率计算
How to prevent Association in cross-border e-commerce multi account operations?
Common functions for PHP to process strings
HTTP cache, forced cache, negotiated cache
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
Solution: système de surveillance vidéo intelligent de patrouille sur le chantier
flask返回文件下载
洛谷P1836 数页码 题解
MFC 给列表控件发送左键单击、双击、以及右键单击消息
Data governance: data quality
JMeter performance test steps practical tutorial
Yu Xia looks at win system kernel -- message mechanism
21. Delete data