当前位置:网站首页>leetcode 203.移除链表元素
leetcode 203.移除链表元素
2022-04-21 06:37:00 【小学五年级在读的蒟蒻】
leetcode 203.移除链表元素
大家好,我是小学五年级在读的蒟蒻,专注于后端,一起见证蒟蒻的成长,您的评论与赞与关注是我的最大动力,如有错误还请不吝赐教,万分感谢。一起支持原创吧!纯手打有笔误还望谅解。
题目描述
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点
题解
遍历节点,如果当前节点的下一个节点的元素是要删除的元素则将当前节点的指针指向下个节点的指针指向
/* * @lc app=leetcode.cn id=203 lang=cpp * * [203] 移除链表元素 */
// @lc code=start
/** * 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 != NULL && head->val == val)
{
ListNode *temp = head;
head = head->next;
delete temp;
}
//删除非头节点的部分
ListNode *index = head;
while (index != NULL && index->next != NULL)
{
if (index->next->val == val)
{
ListNode *temp = index->next;
index->next = temp->next;
delete temp;
}
else
{
index = index->next;
}
}
return head;
}
};
// @lc code=end
题解二
使用虚头节点来遍历链表
在头节点之前再添加一个节点,以添加的节点作为头节点遍历
/* * @lc app=leetcode.cn id=203 lang=cpp * * [203] 移除链表元素 */
// @lc code=start
/** * 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 *vir = new ListNode(0);
vir->next = head;
ListNode *index = vir;
//遍历链表
while (index->next != NULL)
{
//如果遇到删除的元素则移动元素
if (index->next->val == val)
{
ListNode *temp = index->next;
index->next = temp->next;
delete temp;
}
else
{
index = index->next;
}
}
head = vir->next;
return head;
}
};
// @lc code=end
版权声明
本文为[小学五年级在读的蒟蒻]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44229867/article/details/122392125
边栏推荐
- Using Wireshark to restore pcap data stream to picture file
- Login interface universal password bypass
- Testing and Benchmarking
- DOS command kills process
- 中介者模式(3.28-4.3)
- 论文阅读:The Chain of Implicit Trust: An Analysis of the Web Third-party Resources Loading
- Implementation of crud of user management system with JDBC
- Blue Bridge Cup - hex to octal
- Domestic first USBhub Daquan, USB hub2 0,HUB3. 0, Wangjiu prolific, pl2586, ma8601, and xinrunde SL2 2A、SL2. 2s, replace Tang Ming's Fe1 1、FE1. 1s,, Weifeng vl810
- 论文阅读:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi
猜你喜欢
随机推荐
Set up MySQL master-slave replication, read-write separation, one master and one slave
IGMP_ Huawei
【C#】单例模式的实现方案
BGP 自动路由聚合
Ms1836s, HDMI to CVBS, video converter, HDMI receiver, built-in MCU and memory
2020-12-24
【C#】LINQ
状态模式(4.4-4.10)
NP and OSPF default routes
【WPF】级联Combobox
Define a standard class
2020-12-24
NP, OSPF virtual link
Solving 0 / 1 knapsack problem by dynamic programming
Sort method ----- > Hill sort, heap sort
NP、OSPF 缺省路由
Use the security vulnerability detection tool Metasploit to lift the telnet login right of the target metasploitabile2
Detailed explanation of the whole evolution process and architecture design of large websites
关于多线程
論文閱讀:Measuring the Impact of a Successful DDoS Attack on the Customer Behaviour of Managed DNS Servi








