当前位置:网站首页>Likou Brush Question Record 6.1-----203. Remove linked list elements
Likou Brush Question Record 6.1-----203. Remove linked list elements
2022-08-09 02:06:00 【@ Bai Gui】
一、题目
二、代码
/** * 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) {
///统一链表格式
ListNode* dummyhead = new ListNode(0); //设置虚假的头结点 为了使得单节点和多节点情况统一
dummyhead->next = head;
//Define Action Nodes
ListNode* process_node = new ListNode(0);
process_node=dummyhead;
//定义临时节点
ListNode* temp_node = new ListNode(0);
while(process_node->next!=nullptr)
{
if(process_node->next->val==val) //If the next node value of the processing node is equal to val Then change the pointer directly
{
temp_node=process_node->next;
process_node->next=temp_node->next;
}
else
{
process_node=process_node->next;
}
}
std::cout<<"all is finish "<<std::endl;
delete temp_node;
// delete process_node; //和dummyhead指向同一片空间 所以删不掉
return dummyhead->next;
}
};
三、运行结果
边栏推荐
猜你喜欢
谷歌翻译软件-免费谷歌翻译
ONNX是什么?怎么用?[简明解读版]
MT4/MQ4L入门到精通EA教程第二课-MQL语言常用函数(二)-账户信息常用功能函数
Observer pattern
LeetCode每日两题02:轮转数组 (均1200道)
谷歌翻译下载-免费谷歌翻译软件下载
ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
Educational Codeforces Round 132 (Rated for Div. 2)
Wireshark packet capture tool
随机推荐
OpenSceneGraph3.5.1编译
Go-8-Gin framework
etcd实现大规模服务治理应用实战
d初化模板构造器
全文翻译:EDPB 基于设计和默认的数据保护指南
typescript89-展示任务列表功能
数据恢复软件EasyRecovery支持恢复所有类型的文件
Difference between KQL and Lucene
gstreamer 记录
ONNX是什么?怎么用?[简明解读版]
Go-10-模块与包
eladmin容器部署超详细过程
LeetCode每日两题01:二分查找 (均1200道)
力扣刷题记录6.1-----203. 移除链表元素
RF调试过程中现象一
2022 PMP Project Management Certification Exam Registration Guide (1)
MT4/MQL4入门到精通EA课程第二课-常用的功能函数
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
2020.10.13 Development log
Go - 9 - data type - function