#203.   Remove Linked List Elements

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* p=head;
ListNode* pre=new ListNode();
pre->next=head;
ListNode* ret=pre; while(p!=NULL)
{
if(p->val==val)
{
pre->next=p->next;
}
else
{
pre=pre->next;
}
p=p->next;
}
return ret->next;
}
};

Leetcode-203 Remove Linked List Elements的更多相关文章

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

  2. LeetCode 203. Remove Linked List Elements (移除链表中的项)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  3. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. Java for LeetCode 203 Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  5. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  6. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  7. [LeetCode] 203. Remove Linked List Elements 解题思路

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  9. Leetcode 203 Remove Linked List Elements 链表

    去掉链表中相应的元素值 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next ...

  10. [leetcode]203. Remove Linked List Elements链表中删除节点

    这道题很基础也很重要 重点就是设置超前节点 public ListNode removeElements(ListNode head, int val) { //超前节点 ListNode pre = ...

随机推荐

  1. 基于weka的文本分类实现

    weka介绍 参见 1)百度百科:http://baike.baidu.com/link?url=V9GKiFxiAoFkaUvPULJ7gK_xoEDnSfUNR1woed0YTmo20Wjo0wY ...

  2. mybatis 打印sql 语句

    拦截器 package com.cares.asis.mybatis.interceptor; import java.text.DateFormat; import java.util.Date; ...

  3. Java基础语法总结

    1.关键字:每门编程语言都有一些保留的单词,用于定义该门语言,这些单词对编译器有特殊含义,并且不能作为标识符,这就是编程语言的关键字. abstract.boolean.break.byte.case ...

  4. UVAL1277_Cops and Thieves

    单源点汇点无向图,要阻隔某个点的流量,必须在一个点上消耗一定的价值,问你能否在消耗价值不超过k的前提下,阻隔源点到汇点的流量. 直接对于有权值的点拆点,拆后边容量即为点权.其余的点的容量无穷,最大流即 ...

  5. WebService 基本操作

    1.新建asp.net web 应用程序 2.添加web 服务webservice.asmx public string HelloWorld(int a) { if (a==1) { return ...

  6. Debian 8 最小化系统安装muduo

    最近开始学习陈硕的muduo C++ Linux多线程网络库,首先当然是要安装.其间遇到过不少问题,最后不能说都解决了,只能说找到了一条不会遇到什么问题的路线.总结起来是两点: 注意各软件之间要版本匹 ...

  7. iOS ViewController生命周期

    ViewController是view的controller,viewController的职责主要包括管理内部各个view的加载显示与卸载,同时负责与其他ViewController的通信和协调. ...

  8. linux-8 基本命令---echo

    1.echo   命令用于终端显示字符或变量 格式:“echo[字符串| 变量]” @1 .echo命令的字符串输出到终端: @2 .echo查看当前SHELL的变量值(前面有$符号): @3 .查看 ...

  9. SQL语句汇总(二)——数据修改、数据查询

    首先创建一张表如下,创建表的方法在上篇介绍过了,这里就不再赘述. 添加新数据: INSERT INTO <表名> (<列名列表>) VALUES (<值列表>)  ...

  10. Linux 下zip包的压缩与解压

    linux zip 命令详解 功能说明:压缩文件. 语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串& ...