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 --> ...
随机推荐
- 锋利的jQuery初学(4)
css选择器与jQuery选择器 css选择器 符号 说明 用法 #id 选择器 #id{} .class 类选择器 .class{} Element 标签选择器 p{} , 并集选择器 div,p{ ...
- curl请求
<?php $cookie_file = tempnam('./temp','cookie'); //创建cookie文件保存的位置/** * [curl description] * @pa ...
- JVM运行时数据区域
上面已经聊过JVM是什么东东,也谈过了JVM内存的垃圾回收机制.这一篇博客我们来聊聊JVM运行时数据区域. JVM运行时数据区域由5块部分组成,分别是堆,方法区,栈,本地方法栈,以及程序计数器组成. ...
- RobotFramework-RIDE环境搭建一:关于Python2和Python3的共存使用
最近在搭建Robot Framework自动化测试框架,由于Robot Framework 框架是基于Pytho语言开发的,要想使用Robot Framework 首先需要有Python环境. RID ...
- HTML5中的语义标签兼容IE8以及更低版本的浏览器
看某教程,说让HTML5的这些语义标签能够兼容低版本的浏览器,原文是“你可以设置css的display属性为block”.很好理解,就设置css样式为block嘛,那就直接设置咯: header, s ...
- 类似py2exe软件真的能保护python源码吗
类似py2exe软件真的能保护python源码吗 背景 最近写了个工具用于对项目中C/C++文件的字符串常量进行自动化加密处理,用python写的,工具效果不错,所以打算在公司内部推广.为了防止代码泄 ...
- Qt creator使用笔记
设置头文件的搜索路径编辑项目文件 xxx.pro INCLUDEPATH = /src/doip \ /src/doip/utils \ /src/doip/pduR \ /src/doip/uds1 ...
- Thing in java 第四章,控制执行流程,练习题答案
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Writ ...
- 提示-bash: telnet: command not found的解决方法
Linux centos 运行telnet命令,出现下面的错误提示: [root@localhost ~]# telnet 127.0.0.1 9501 -bash: telnet: command ...
- Flink神秘工具lib
Flink里面有一个神坑,叫做FI坑.其实只是使用Fi的时候被暴露出来.但是,杀不死你的,终将使你更加强大. Flink集群有一个lib文件件,里面比较happy,可以放各种jar:这样,client ...