LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素。
思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作。我做不出来。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while(head&&head->val==val) head=head->next;
if(!head) return ;
ListNode *tmp=head;
while(head&&head->next)
{
if(head->next->val==val)
head->next=head->next->next;
else
head=head->next;
}
return tmp;
}
};
Remove Linked List Elements
LeetCode Remove Linked List Elements 删除链表元素的更多相关文章
- 203 Remove Linked List Elements 删除链表中的元素
删除链表中等于给定值 val 的所有元素.示例给定: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6返回: 1 --& ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] 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 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- 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题要求 ...
- [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
Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...
- 203. Remove Linked List Elements - LeetCode
Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...
- 【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 ...
随机推荐
- linux正则表达式基础
linux中awk,sed,grep等 命令使用区别正则表达式基础 在最简单的情况下,一个正则表达式看上去就是一个普通的查找串.例如,正则表达式"testing"中没有包含任何元字 ...
- 20.Consent Controller Get请求逻辑实现
在这里之前讲的这一块的信息就要登场了 需要通过构造函数把这几个注入进来 县引入这三个命名空间 把这三个注入进来,这就是显示依赖 先通过returnUrl拿到这个Request request拿到后,就 ...
- js用"."和"[]"获取属性的区别
在JavaScript中通常使用”."运算符来存取对象的属性的值.或者使用[]作为一个关联数组来存取对象的属性. 对象的属性和方法统称为对象的成员. 访问对象的属性 在JavaScript中 ...
- AutoHotkey常用配置
; 开发常用 ^e:: run D:\soft\java\MyEclipse for Spring 2014\myeclipseforspring.exe return ^d:: run D:\sof ...
- poj1144 tarjan求割点
poj1144 tarjan求割点 额,算法没什么好说的,只是这道题的读入非常恶心. 注意,当前点x是否是割点,与low[x]无关,只和low[son]和dfn[x]有关. 还有,默代码的时候记住分目 ...
- 2017-9-2 NOIP模拟赛
“与” (and.pas/.c/.cpp) 时间限制:1s:空间限制64MB 题目描述: 给你一个长度为n的序列A,请你求出一对Ai,Aj(1<=i<j<=n)使Ai“与”Aj最大. ...
- Solidity 最新 0.5.8 中文文档发布
本文首发于深入浅出区块链社区 热烈祝贺 Solidity 最新 0.5.8 中文文档发布, 这不单是一份 Solidity 速查手册,更是一份深入以太坊智能合约开发宝典. 翻译说明 Solidity ...
- [Android]Android性能优化
安卓性能优化 性能优化的几大考虑 Mobile Context 资源受限 + 内存,普遍较小,512MB很常见,开发者的机器一般比用户的机器高端 + CPU,核心少,运算能力没有全开 + GPU,上传 ...
- BZOJ-3555:企鹅QQ(字符串哈希)
PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体验,满足用 ...
- MCS-51单片机的定时器/计数器概念
一.MCS-51单片机的定时器/计数器概念 单片机中,脉冲计数与时间之间的关系十分密切,每输入一个脉冲,计数器的值就会自动累加1,而花费的时间恰好是1微秒;只要相邻两个计数脉冲之间的时间间隔相等,则计 ...