Lintcode452-Remove Linked List Elements-Easy
Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
Example
Example 1:
Input: head = 1->2->3->3->4->5->3->null, val = 3
Output: 1->2->4->5->null
Example 2:
Input: head = 1->1->null, val = 1
Output: null
思路:
函数参数中的ListNode head是链表中的第一个节点。所以要先加入头节点dummy, 并使head变为头节点(line 4)。(头节点指向链表的第一个节点)
加入头节点有两个作用:
- dummy node 始终指向链表的第一个节点,这样返回整个链表只需要dummy.next
- head 作为头节点,使对链表第一个节点的操作(插入,删除等)和链表内其他节点相同,不用单独考虑第一个节点操作的特殊性。
错误示范:
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
}
head = head.next;
}
可能抛出NullPointerException异常。比如 Input: head = 1->2->3->3->4->5->3->null, val = 3。
原因:
这个题是一个if-else case: 要么head结点的下一个结点值等于要删除的值,要么不等于。
如果是第一种情况,那么在改变head.next属性后,head节点不需要向下移动一个。因为此时head.next 属性已经改变,需要重新判断 head.next != null 和 head.next.val ?= val
正确代码:
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/ public class Solution {
/**
* @param head: a ListNode
* @param val: An integer
* @return: a ListNode
*/
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
} else {
head = head.next;
}
}
return dummy.next;
}
}
Lintcode452-Remove Linked List Elements-Easy的更多相关文章
- LeetCode--LinkedList--203. Remove Linked List Elements(Easy)
203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...
- LeetCode_203. Remove Linked List Elements
203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- [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-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements
237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...
- 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 Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
随机推荐
- redis相关问题
什么是Redis?Redis 是一个使用 C 语言写成的,开源的 key-value 数据库..和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表 ...
- 设计模式之State(状态)(转)
State的定义: 不同的状态,不同的行为;或者说,每个状态有着相应的行为. 何时使用? State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif ...
- c# ref和out参数
向方法传递参的时候,对应的参数通常会用实参的拷贝来初始化.就是说随便在方法内部进行怎样的修改,都不会影响作为参数传递的变量的原始值. 通过上面的例子我们可以看出来,如果一个方法的参数是引用类型,那么使 ...
- 转:C#清除回收站
SHEmptyRecycleBin是一个内核API方法,该方法能够清空回收站中的文件,该方法在C#中需要手动的引入方法所在的类库.该方法在C#中的声明语法如下: [DllImportAttrbute( ...
- oracle函数,游标,视图使用总结
oracle函数或者叫存储过程,在实际的开发过程中对于复杂的业务需求是非常有用的,非常有效率的也是非常好玩儿的一个技术点. 平常在开发过程中对于CRUD功能较多.一般SQL即可应付,大不了就是长一点而 ...
- element ui里dialog关闭后清除验证条件
//vue <!--添加用户dialog begin--> <el-dialog title="编辑用户" :visible.sync="dialogF ...
- PLSQL 问题小记
问题1:在一个拼写长字符串的函数中,若出现ora-06502或者ora-06512的错误,则有可能是在拼串的过程中,数据类型的隐式转换出了问题,如:字符类型转为数字,此时会出现错误. 解决方案:在拼串 ...
- Hybrid App中原生页面 VS H5页面(分享)
本文部分转自 http://www.jianshu.com/p/00ff5664e000 现有3类主流APP,分别为:Web App.Hybrid App(混合模式移动应用,Hybrid有“混合的” ...
- nmap扫描验证多种漏洞
nmap在Kali Linux中是默认安装的.它不仅可以用来确定目标网络上计算机的存活状态,而且可以扫描各个计算机的操作系统.开放端口.服务,还有可能获得用户的证书. 命令结构: nmap -sS - ...
- AdminLTE模板使用
AdminLTE介绍: AdminLTE是一款建立在bootstrap和jquery之上的开源的模板主题工具,它提供了一系列响应的,可重复使用的组件,并内置了多个模板页面;同时自适应多种屏幕分辨率,兼 ...