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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null) return head;
ListNode tmp=head;

while(tmp!=null && tmp.val==val){
tmp=tmp.next;
head=head.next;
}
ListNode p=tmp;
if(tmp!=null)
tmp=tmp.next;
while(tmp!=null){
if(tmp.val==val){
p.next=tmp.next;
tmp=tmp.next;
}else{
p=p.next;
tmp=tmp.next;
}
}
return head;

}
}

运行结果:

(easy)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. Java [Leetcode 203]Remove Linked List Elements

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

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

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

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

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

  8. Leetcode 203 Remove Linked List Elements 链表

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

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

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

随机推荐

  1. TKinter布局之grid 网格布局

    1.由于我们的程序大多数都是矩形,因此特别适合于网格 布局,也就是 grid 布局. 2.使用 grid 布局的时候,我们使用 grid 函数,在里面指 定两个参数,用 row 表示行,用 colum ...

  2. 目录操作工具类 CopyDir.java

    package com.util; import java.io.*; /** * 1,建立目的目录. 2,遍历源目录. 3,遍历过程中,创建文件或者文件夹. 原理:其实就是改变了源文件或者目录的目录 ...

  3. mysql toolkit 用法[备忘] (转)

    命令列表 /usr/bin/pt-agent /usr/bin/pt-align /usr/bin/pt-archiver /usr/bin/pt-config-diff /usr/bin/pt-de ...

  4. redis 服务访问密码设定

    1. 更改redis.conf配置 # requirepass foobared 去掉注释,foobared改为 自己的password , 我测试的时候用的是 redis-password 2.启动 ...

  5. HTML之布局position理解

    HTML中的布局,一个比较难以理解的概念,就是position了,W3C上的解释,position有如下几种: 1. static,默认值,也就是在样式中不指定position 2. fixed 3. ...

  6. CSS3 Media Queries(响应式布局可以让你定制不同的分辨率和设备)

    点评:Media Queries这功能是非常强大的,他可以让你定制不同的分辨率和设备,并在不改变内容的情况下,让你制作的web页面在不同的分辨率和设备下都能显示正常,并且不会因此而丢失样式   Med ...

  7. bootstrap的datetimepicker只选择月份

    本文转载自:http://blog.csdn.net/feng1603/article/details/41869523 直接上代码: //选择年月日的 startView: 2, minView: ...

  8. (转)Edge实现NodeJS与.NET互操作(包括UI界面示例)

    本文转载自:http://blog.csdn.net/kimmking/article/details/42708049 1.  Edge是什么 Edge是一种在进程内实现NodeJS与.NET互操作 ...

  9. Python 字典和列表的对比应用

    Q:将下列格式的txt文件,打印出该选手的3个最快跑步时间 james2.txt =>“James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:0 ...

  10. perl语言书籍教程推荐

    互动出版网计算机频道.为您推荐关于perl语言的书籍教程.包括perl push.perl chomp以及perl python等perl语言内容. perl语言书籍一.<Perl语言编程 第四 ...