Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
/**
* 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) { ListNode node(-);
node.next=head;
ListNode *pre=&node;
while(head!=NULL)
{
if(head->val==val)
{
pre->next=head->next;
delete head;
head=pre->next;
}
else
{
pre=pre->next;
head=head->next;
} }
return node.next;
}
};
Remove Linked List Elements的更多相关文章
-
[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. ...
-
203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
-
LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
-
LeetCode_203. Remove Linked List Elements
203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...
-
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
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
随机推荐
-
JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
前言:之前博主分享过knockoutJS和BootstrapTable的一些基础用法,都是写基础应用,根本谈不上封装,仅仅是避免了html控件的取值和赋值,远远没有将MVVM的精妙展现出来.最近项目打 ...
-
iOS简单实现毛玻璃效果
iOS8之后有一个类 UIVisualEffectView // 毛玻璃 UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMak ...
-
CF570D:Tree Requests
传送门 DFS重标号+二分 打比赛的时候想了很多方法..DFS序,BFS序,倍增什么的都考虑了一遍,但是几乎要么是可以维护两个区间但是代码复杂度爆炸,要么就是只能维护单一维度的信息. 这道题的具体做法 ...
-
20161106PM-Fiddler
1. 设置Fidder使之支持HTTPS协议 Tools->Fiddler Options->HTTPS->勾上Decrypt HTTPS traffic->OK 2. 断点 ...
-
Undefined symbols “_OBJC_CLASS_$_XXX” 问题
解决方法是点击工程,在targets界面中找到Build Phases,根据提示信息“XXX”来判断缺少什么文件,一般如果缺少自定义的文件,XXX会是缺少的类名,那么就在Complie Sources ...
-
highchart几个图表马金摘要
1) Js的引入顺序不对,导致highchart的图表出不来. 由于Highchart插件中用到了jquery,jquery的js要在引入highchart的js之前引入,否则当会导致当载入high ...
-
dict的几个要点
1. 采用key,value键-值对进行存储 2. key必须是不可变对象 3. key值不能重复 添加元素: aDict = {'1':'aaa','b':'bbb','3':'ccc'} aDic ...
-
[luogu1341][无序字母对]
luogu1341 思路 欧拉回路和欧拉路的裸题,首先判断是否存在欧拉路或者欧拉回路.当且仅当途中每个点的度数都为偶数时,存在欧拉回路.当且仅当图中度数为奇数的点的个数为2时,存在欧拉路.如果存在欧拉 ...
-
2.5 定义FTP工具的各种方法
用class定义ftp工具的各种方法 import os,sys from ftplib import FTP from mimetypes import guess_type,add_type fr ...
-
Case when then esle end
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...