LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val.
Example:
Input: ->->->->->->, val =
Output: ->->->->
本题的思路很简单,就是单纯的删除链表元素操作,如果头结点的元素就是给定的val值,需要借助虚结点dummy去判断。
方法一:(C++) C++中注意结点的释放
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummy=new ListNode(-),* pre=dummy;
pre->next=head;
while(pre->next){
if(pre->next->val==val){
ListNode* t=pre->next;
pre->next=pre->next->next;
t->next=NULL;
delete t;
}
else
pre=pre->next;
}
return dummy->next;
}
Java:
public ListNode removeElements(ListNode head, int val) {
if(head==null||(head.next==null&&head.val==val))
return null;
ListNode dummy=new ListNode(-1),cur=dummy;
cur.next=head;
while(cur.next!=null){
if(cur.next.val==val)
cur.next=cur.next.next;
else
cur=cur.next;
}
return dummy.next;
}
方法二:不使用虚结点的话,最后只需要再判断一下头结点的值是否符合要求(C++)
ListNode* removeElements(ListNode* head, int val) {
if(head==NULL)
return head;
ListNode* cur=head;
while(cur->next){
if(cur->next->val==val)
cur->next=cur->next->next;
else
cur=cur->next;
}
return (head->val==val)?head->next:head;
}
LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java的更多相关文章
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java for LeetCode 203 Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
随机推荐
- Spring mvc session cookie实现自动登录
设计过程 1. user表存储用户名密码等信息,login表存放用户登陆状态的表 user表中存储username,pwd,等信息 login表存username,series(UUID),token ...
- HOMEWORD2
开发工具和语言选择 语言 :pyhton3.6 工具 我选择的是 pycharm2019.1.由于之前已经安装好了,这里就不再贴出. 自动单元测试练习 python单元测试框架Unittest.Uni ...
- SQL Sever 2012版本数据库的完全卸载
首先再使用的过程中,遇到当前版本和项目数据库服务器的SQL Sever 版本不一致,导致无法正常的数据导入. 所以需要我们将本地的SQL Sever 数据库,进行一个完整的卸载,进而去安装和项目一致的 ...
- 《DSP using MATLAB》Problem 7.27
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- 序列化、time、random、hashlib、sys模块
•很多常用和内置模块,我们只需要掌握他们的用法而暂时不用考虑内部是如何实现的,这些模块大大提升了开发效率 ! 1.json模块与pickle模块 •json 如果你有这样的困扰,当希望把一种数据存到硬 ...
- Hello_World
简单A+B #include <stdio> int main() { int a,b; scanf("%d%d",&a, &b); printf(&q ...
- bookmarks
嵌入式/硬件/计算机原理 PCI ID的分配 ARM汇编指令介绍 (连续4篇) https://blog.csdn.net/makethyme/article/details/1641413https ...
- 利用工具将数据库中的表导出到word中
1.动软代码生成器 效果图: 数据库设计说明书中的一项,刚好我负责写这个文档, 18张表,前两张表是自己画表格自己填充内容,写到第三张表的时候就已经崩溃了(我觉得我耐力还是够的,怎么说也画完了两张表呢 ...
- debian 安装libreoffice6.1 转换pdf
这个东西能转,但效率不高,我已经用专门的服务器docker,速度很快,直接用curl就能返回 但这里还是记录一下直接用命令调用吧,毕竟有的老的地方这么用 && cd /usr/loca ...
- C#使用NPOI读写Excel的注意事项
NPOI的基本使用参照:https://www.cnblogs.com/lixiaobin/p/NPOI.html 既存文档读取修改方法 *既存Excel文档修改保存注意使用FileMode.Crea ...