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 --> ...
随机推荐
- 让Entity Framework不再私闯sys.databases
这里的“私闯sys.databases”是指Entity Framework默认发起的查询:SELECT Count(*) FROM sys.databases WHERE [name]=N'数据库名 ...
- bootstrap研究感想1
我—>新人,特纯的新人,受到方大神的建议,开始写博客,写一些工作时敲代码时的感受,学习模仿大神时的感悟. -------------------------------------------- ...
- ECharts 从后台动态获取数据 (asp.net)
(一) 使用工具 visual studio 2017:Web开发:asp.net (代码中的js引用路径以及ajax方法调用的url,记得修改哦) (二) 准备工作(此处写给和我一样小白) 1.动态 ...
- CRM-stark组件
stark组件 1. stark也是一个app(用startapp stark创建),目标时把这个做成一个可以拔插的组件 2. setting文件下INSTALLED_APPS 路径要配置好(app的 ...
- AllocateHWnd SetTimer API
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 使用PROC TRANSPOSE过程步对数据集进行转置时如何保持日期变量的时间顺序
有一个数据集如下所示: 如果直接进行转置. SAS程序: proc transpose data=test out=outx1 (drop=_name_); by id; var amount; id ...
- 通用唯一识别码UUID
UUID 概念:UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,目前最广泛应用的UUID,是微软公司的全局唯一标识符(GUID),而其他重要的应用,则 ...
- Unity3D外包 团队更新一下UE4和Unity3D案例
欢迎联系我们索取,谢谢! 有项目外包请联系QQ:372900288 索取案例.
- laravel学习资料
http://blog.csdn.net/qq_20873039/article/category/6246852 --laravel核心概念 /Eloquent ORM / laravel bl ...
- Opencv 图像读取与保存问题
转自 @yhl_leo 1 图像读取 首先看一下,imread函数的声明: // C++: Mat based Mat imread( ); // C: IplImage based IplImage ...