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

  

(LinkedList) Remove Linked List Elements的更多相关文章

  1. (LeetCode 203)Remove Linked List Elements

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

  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. [LintCode] Remove Linked List Elements 移除链表元素

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

  4. Leetcode-203 Remove Linked List Elements

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

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

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

  7. 【LeetCode】203. Remove Linked List Elements

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

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

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

  9. LeetCode_203. Remove Linked List Elements

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

随机推荐

  1. android通知-Notification

    android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...

  2. 使用ViewPager实现屏幕滑动效果

    oncreate中设置viewPager viewPager.setPageTransformer(true,new DepthPageTransformer());//设置页面过滤动画效果 page ...

  3. 5、Linux下面桌面的安装

    搭建本地yum仓库的方法 http://www.cnblogs.com/lql123/p/5952788.html 1.yum grouplist        (列出yum仓库里的软件组列表) .y ...

  4. Java String字符串补0或空格

    package cn.com.songjy; import java.text.NumberFormat; //Java 中给数字左边补0 public class NumberFormatTest  ...

  5. hdu 2079

    ps:昨天刚做了个母函数的,觉得不太熟,今天又是母函数..很好.. 代码: #include "stdio.h" #include "string.h" ]; ...

  6. C#—打开文件

    一.打开图片 ps:必须先在pictureBox中导入一张图片 private void button4_Click(object sender, EventArgs e)//选择.更换图片 { if ...

  7. 在maven项目中解决第三方jar包依赖的问题

    在maven项目中,对于那些在maven仓库中不存在的第三方jar,依赖解决通常有如下解决方法: 方法1:直接将jar包拷贝到项目指定目录下,然后在pom文件中指定依赖类型为system,如: < ...

  8. 【转载】 postman使用教程

    一 接口请求流程     二 postman使用   从流程图中我们可以看出,一个接口请求需要设置:请求URL,请求方法,请求头,请求参数.同样的,在postman中,我们也只需要设置这四项即可完成一 ...

  9. C#项目实例中读取并修改App.config文件

    C#项目是指一系列独特的.复杂的并相互关联的活动,这些活动有着一个明确的目标或目的,必须在特定的时间.预算.资源限定内,依据规范完成.项目参数包括项目范围.质量.成本.时间.资源. 1. 向C#项目实 ...

  10. Intellij IDEA连接Git@OSC

    错误提示:fatal: remote origin already exists. 解决办法:$ git remote rm origin http://my.oschina.net/lujianin ...