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.
* 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 null; while(head.val==val&&head.next!=null)
head=head.next; if((head.next==null&&head.val==val))
return null; ListNode p=head; while(p.next!=null)
{
if(p.next.val==val)
p.next=p.next.next;
else p=p.next; }
return head;
}
}

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. 203. Remove Linked List Elements【easy】

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

  3. 203. Remove Linked List Elements - LeetCode

    Question 203. Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 ...

  4. 【LeetCode】203. Remove Linked List Elements

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

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

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

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

  8. Java [Leetcode 203]Remove Linked List Elements

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

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

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

随机推荐

  1. 迭代输出Map和List<Map<String,Object>>的方法

    一.Map<String,Object> String:key的类型 Object:value的类型,value可能是String,或者int类型,什么类型都可以 对于Map接口来说,本身 ...

  2. Linux-Unix版本介绍

    转自: http://blog.163.com/li_zhuangs/blog/static/195698098201182411360635/ < DOCTYPE html PUBLIC -W ...

  3. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  4. 【海量视频】2013年上半年BPM厂商'K2'市场活动资料集锦

    3月01日         中广核K2 &SAP流程解决方案分享 活动报道:http://www.k2software.cn/k2events_content/items/k2-sap-346 ...

  5. NOP初学记录

    1.  介绍的话不多说了.直接先来简单的安装跟配置先以3.6版本为例: 附带官网地址: http://www.nopcommerce.com   自行下载. 中文网:http://www.nopchi ...

  6. 到目前为止,Linux下最完整的Samba服务器配置攻略 (转)

    http://blog.chinaunix.net/uid-23069658-id-3142052.html 安装平台为UBUNTU 14.04,直接软件中心安装samba, service smb ...

  7. 百度地图开发 android App 数字签名(SHA1)获取办法

    简述: a.输入keytool -list -v -keystore debug.keystore,会得到三种指纹证书,选取SHA1类型的证书(密钥口令是android),这个获取到的SHA1的值和e ...

  8. vi notes

    x = wqqq!, quit without save. movej,h,k,l^ or 0: start of line$: end of line:0, start of file:$, end ...

  9. 关于WinForm引用WPF窗体---在Winform窗体中使用WPF控件

    项目中有个界面展示用WPF实现起来比较简单,并且能提供更酷炫的效果,但是在WinForm中使用WPF窗体出现了问题,在网上找了一下有些人说Winform不能引用WPF的窗体,我就很纳闷,Win32都能 ...

  10. mycat启动后,用Navicat Premium 连接报 "2013"

    最近在学习mycat,启动后,用Navicat Premium 连接报 "2013"  Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...