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* ...
随机推荐
- 排名函数——ROW_NUMBER()、RANK()、DENSE_RANK()和NTILE(n)
ROW_NUMBER()函数:行号,根据作为参数传递给这个函数的ORDER BY子句的值,返回一个不断递增的整数值.如果ROW_NUMBER的ORDER BY的值和结果集中的顺序相匹配,返回值将是递增 ...
- golang学习笔记8 beego参数配置 打包linux命令
golang学习笔记8 beego参数配置 打包linux命令 参数配置 - beego: 简约 & 强大并存的 Go 应用框架https://beego.me/docs/mvc/contro ...
- API gateway 之 kong 安装
kong安装: https://getkong.org/install/centos/ 下载指定版本rpm: wget https://bintray.com/kong/kong-community- ...
- C#简单线程
一.实例1 static void Main(string [] args) { Console.WriteLine("开始线程"); startFunc(); Console.W ...
- linux系统Centos环境下搭建SVN服务器及权限配置
linux系统Centos环境下如何搭建SVN服务器以及svnserve.conf.authz.passwd配置文件详细介绍 至于svn的概念,这里就不做详细阐述了,可以自行百度.简单来讲就是一个 ...
- MXNet官方文档中文版教程(3):神经网络图(Symbol)
https://blog.csdn.net/qq_36165459/article/details/78394259 文档英文原版参见Symbol - Neural network graphs an ...
- kivy Properties
Introduction to Properties¶ Properties are an awesome way to define events and bind to them. Essenti ...
- Python3.5+PyQt5多线程+itchat实现微信防撤回桌面版代码
weChatThread线程类 之前一直不会python多线程,写这个程序的时候,发现不用多线程会陷入无限未响应状态.于是学了半天python多线程,但是在主函数里写的时候,发现一个问题,Ui主线程和 ...
- Centos6下给PHP安装Qconf扩展
一.下载Qconf的zip包 1.进入github地址:https://github.com/Qihoo360/QConf,下载zip包文件,并上传至服务器二.安装QConf unzip ...
- P2801 教主的魔法(分块)
P2801 教主的魔法 区间加法,区间查询 显然就是分块辣 维护一个按块排好序的数组. 每次修改依然是整块打标记,零散块暴力.蓝后对零散块重新排序. 询问时整块二分,零散块暴力就好辣 注意细节挺多和边 ...