leetcode 203. Remove Linked List Elements(链表)
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
题解:链表的操作有两个常用的技巧:第一就是用递归,第二就是开一个新节点指向头指针来方便一些操作。
本题也是递归和非递归两种方法:
非递归:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* removeElements(ListNode* head, int val) {if(head==nullptr)return head;ListNode* p=new ListNode(0);p->next=head;ListNode* cur=p;while(cur&&cur->next){if(cur->next->val==val){cur->next=cur->next->next;}else{cur=cur->next;}}return p->next;} };
这里有一个值得思考的地方:在leetcode 237. Delete Node in a Linked List这道题中,我们不知道一个节点的前驱节点,但是也可以通过让该节点等于它后一个节点的方式间接的删除该节点。那么这道题可不可以这样呢?
答案是否定的,因为那道题有一个限制是:要删除的节点肯定不是最后一个节点。因为如果是最后一个节点,我们就必须通过把它的前驱节点的next指针置为空这种方式来删除最后一个节点,而不能使用使该节点等于它后一个节点的方式来删除该节点了。
而本题确实存在要删最后一个节点的情况,所以不能用那道题的方法。其实那种方法也不具有普遍性,删除链表的一个节点就是用本题的方法就好。
递归的方法:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* removeElements(ListNode* head, int val) {if(head==nullptr){return head;}if(head->val==val){return removeElements(head->next,val);}head->next=removeElements(head->next,val);return head;} };
转载于:https://www.cnblogs.com/zywscq/p/5429168.html
leetcode 203. Remove Linked List Elements(链表)相关推荐
- [leetcode]203. Remove Linked List Elements链表中删除节点
这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) {//超前节点ListNode pre = n ...
- LeetCode 203. Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- leetcode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have valueval. Example Given: 1 --> 2 ...
- leetcode python3 简单题203. Remove Linked List Elements
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零三题 (1)题目 英文: Remove all elements from ...
- Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- (LeetCode 203)Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- C#LeetCode刷题之#203-删除链表中的节点(Remove Linked List Elements)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3826 访问. 删除链表中等于给定值 val 的所有节点. 输入: ...
- [LeetCode]Remove Linked List Elements
题目描述:(链接) Remove all elements from a linked list of integers that have value val. Example Given: 1 - ...
最新文章
- Kubernetes 安装
- ABAP动态取得数据的方法
- mybatis使用全注解的方式案例(包含一对多关系映射)
- java pair class,在Java Pair Class Tuple中获取值
- prometheus修改数据保留时间
- vscode 执行npm命令_生产力终极指南:用了两年,如今才算真正会用VS Code
- javaSE----继承
- 盐城大数据产业园人才公寓_盐城市大数据产业园获评大众创业万众创新示范基地...
- 构建REST风格的Web Service (转)
- 解决maven不能自动导入ojdbc14的问题
- 计算机cad图块,CAD图块的操作及概念
- 手把手教你用ArcGIS做张降雨量分布专题图
- 数据结构与算法分析----顺序表
- XMind2TestCase 库的使用及自定义导出文档的格式
- linux 蓝牙打印机驱动安装失败,蓝牙驱动安装失败如何解决_蓝牙驱动安装不了怎么处理...
- 2022最火的Linux性能分析工具--perf
- mysql查询的优化
- 嵌入式Web项目(一)——Web服务器的引入
- linux 私有云存储,私有云存储搭建(owncloud)
- UI5-Fiori初学者导航
热门文章
- org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArra
- oracle 常用调优方法
- MVC 3 基本操作增加修改
- 提高SQL执行效率的几点建议
- 一键搞定数码照片印前特效-【用可牛影像】
- 保证Web数据库安全 认真把好七道关
- Fastformer:简单又好用的Transformer变体!清华MSRA开源线性复杂度的Fastformer!
- CV Papers|计算机视觉论文推荐周报20200502期
- 重磅!TensorFlow 2.0 来了!
- 【资源下载】DeepMindUCL深度学习与强化学习进阶课程