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的更多相关文章

  1. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  2. Leetcode-203 Remove Linked List Elements

    #203.   Remove Linked List Elements Remove all elements from a linked list of integers that have val ...

  3. 【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 ...

  4. 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题要求 ...

  5. 【LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. LeetCode Remove Linked List Elements 删除链表元素

    题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...

  8. LeetCode_203. Remove Linked List Elements

    203. Remove Linked List Elements Easy Remove all elements from a linked list of integers that have v ...

  9. LeetCode--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

  10. 【刷题-LeetCode】203. Remove Linked List Elements

    Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...

随机推荐

  1. impala简单使用

    impala-shell connect ha1:21000 更新元信息 invalidate metadata;

  2. Arch命令行与安装包

    >>mkfs.vfat                                                                     # pacman -S do ...

  3. strong & weak

    Here is a quick summary: A strong reference will keep the object it points to from being deallocated ...

  4. ArchLinux KDE安装中文输入法

    From: http://www.linuxdiyf.com/viewarticle.php?id=53375 1.安装中文输入法#pacman -S scim-pinyin #拼音输入法#pacma ...

  5. 树莓派_360wifi2_佳能MP236打印机

    入手树莓派后一直没时间弄,设想用360wifi做无线网卡,也一直不得解,今天成功写下经验 本人刷的是官方系统,版本为3.12.28,首先更新系统内核,参考以下网址: http://groenholdt ...

  6. Linux学习笔记——查看Linux系统信息的方法

    由于Linux的发行版本比较多,并且有些差异性,所以,分析问题时我们常常需要知道自己的Linux系统的版本信息,以下是我搜集到的一些方法 1:显示电脑以及操作系统的相关信息 qian@ubuntu:~ ...

  7. delphi action学习

    procedure TEditAction.UpdateTarget(Target: TObject); begin if (Self is TEditCut) then Enabled := (Ge ...

  8. Hadoop HDFS编程 API入门系列之HdfsUtil版本2(七)

    不多说,直接上代码. 代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs1; import java.io.FileInputStream;import ...

  9. DIRECTORY_SEPARATOR 和 PATH_SEPARATOR的区别

    DIRECTORY_SEPARATOR:目录分隔符,linux上就是’/’    windows上是’\’ PATH_SEPARATOR:路径分隔符,include多个路径使用,在win下,当你要in ...

  10. Operate blob data in Oracle via C#

    oracle table: CREATE TABLE "SCOTT"."TEST_BLOB"    (    "NAME" VARCHAR2 ...