当前位置:网站首页>2.1 链表——移除链表元素(Leetcode 203)
2.1 链表——移除链表元素(Leetcode 203)
2022-06-12 07:58:00 【Amoni_】
直接使用原来的链表来进行移除节点操作:
/** * 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) {
// 删除头结点
while(head != nullptr && head->val == val){
// 不等于空指针 且 头结点值等于val
ListNode *temp = head;
head = head->next;
delete temp;
}
// 删除非头节点
ListNode *cur = head; // 初始化当前指针为头指针
while(cur != nullptr && cur->next != nullptr){
// 不等于空指针 且 不是尾节点
if(cur->next->val == val){
// 头结点已经进行过判断,因此判断下一个节点的val
ListNode *temp = cur->next;
cur->next = cur->next->next;
delete temp;
}
else{
cur = cur->next;
}
}
return head;
}
};
设置一个虚拟头结点在进行移除节点操作:
/** * 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) {
// 创建并初始化dummyHead
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
// 初始化当前指针为dummyHead
ListNode* cur = dummyHead;
while(cur->next != nullptr){
if(cur->next->val == val){
ListNode* tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
}
else{
cur = cur->next;
}
}
// 还原Head,并删除dummyHead
head = dummyHead->next;
delete dummyHead;
return head;
}
};
总结:设置一个虚拟头结点,使得头结点变成了普通节点,操作上变得更简单一致
边栏推荐
- Process terminated
- 连接数据库却无法修改数据
- Parameter estimation of Weibull distribution
- MinGW offline installation package (free, fool)
- C # hide the keyboard input on the console (the input content is not displayed on the window)
- Windows10 configuration database
- Numerical calculation method chapter5 Direct method for solving linear equations
- 2D visualization of oil storage, transportation and production, configuration application enables intelligent development of industry
- Clarify the division of IPv4 addresses
- Visual studio code batch comment and uncomment
猜你喜欢

Chapter V - message authentication and digital signature

Topic 1 Single_Cell_analysis(2)

Improvement of hash function based on life game (continued 2)

Classic paper review: palette based photo retrieval

Symfony 2: multiple and dynamic database connections

Voice assistant - potential skills and uncalled call technique mining

Scoring prediction problem

谋新局、促发展,桂林绿色数字经济的头雁效应

HDLC protocol

Voice assistant -- Qu -- semantic role annotation and its application
随机推荐
Servlet
Leetcode notes: Weekly contest 295
Classic paper review: palette based photo retrieval
Web page performance optimization interview questions
tar之多线程解压缩
The R language catools package divides the data, the scale function scales the data, the KNN function of the class package constructs a k-nearest neighbor classifier, and compares the model accuracy u
Compiling principle on computer -- function drawing language (III): parser
Compiling principle on computer -- functional drawing language (IV): semantic analyzer
Principle and example of OpenMP task
Numerical calculation method chapter5 Direct method for solving linear equations
Meter Reading Instrument(MRI) Remote Terminal Unit electric gas water
Process terminated
"Three.js" auxiliary coordinate axis
20220526 yolov1-v5
20220524 backbone deep learning network framework
20220526 loss function
C # hide the keyboard input on the console (the input content is not displayed on the window)
LeetCode笔记:Weekly Contest 296
Leetcode notes: Weekly contest 276
Topic 1 Single_ Cell_ analysis(3)