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

题目大意:给定一个链表,删除所有等于指定的值的元素。

解题思路:这种题目肯定要一次遍历才能过,记录前驱节点,当下一个节点等于指定的值的时候,继续循环找到不等的x节点,然后将前驱的next节点赋为x。

Talk is cheap>>

    public ListNode removeElements(ListNode head, int val) {
if(head==null){
return null;
}
while(head!=null&&head.val==val){
head=head.next;
}
ListNode ptr = head;
while(ptr!=null){
ListNode pre = ptr;
ptr=ptr.next;
while(ptr!=null&&ptr.val==val){
ptr=ptr.next;
}
pre.next = ptr;
}
return head;
}

Remove Linked List Elements——LeetCode的更多相关文章

  1. 203. Remove Linked List Elements - LeetCode

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

  2. Remove linked list elements | leetcode

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

  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. LeetCode Remove Linked List Elements 删除链表元素

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

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

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

  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. Eclipse 打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素(转)

    打开 Eclipse 时,如图所示: 解决办法: 把有问题的 devices.xml 文件删除,再把 sdk 里面 tools\lib 下的这个文件拷贝到你删除的那个文件夹里,重启 eclipse 就 ...

  2. 解决kernel headers报错

    Make sure you have updated version $ sudo apt-get update Search for kernel version (optional) $ apt- ...

  3. xargs rm -rf 与 -exec rm

    # find ./ -exec rm {} \; # find ./ | xargs rm -rf 两者都可以把find命令查找到的结果删除,其区别简单的说是前者是把find发现的结果一次性传给exe ...

  4. Eclipse中看java源代码

    如何在Eclipse sdk中查看jar源代码如:*.jar 1.点 “window”-> "Preferences" -> "Java" -> ...

  5. NodeJS学习笔记—1.CommonJS规范

    由于现在web开发,越来越重视代码的复用和抽象的封装,为了解决代码的组织结构.管理.复用和部署等问题,现在普遍采用的机制是模块机制(module).CommonJS约定桌面应用程序和服务器应用程序需要 ...

  6. IOC之Unity

    项目最近用到,查找到比较好的资料,记录哈: Unity 3(一):简介与示例: Unity 3(二):Unity在AOP方面的应用

  7. NSNumber 转 NSString

    之前number 转string时候调用stringValue,后来发现未完全转 NSNumber * a_num = [NSNumber numberWithInteger: ]; NSString ...

  8. [转]dos命令 cd命令使用说明[图文说明]

    Cddir(change directory,可以缩写为cd),其功能是显示当前目录的名称,或更改当前的目录. 应用时公带一个驱动器号(如: cd c:)在命令行cmd中输入 cd /? 可显示帮助信 ...

  9. 226. Invert Binary Tree(C++)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  10. python split()黑魔法

    split()用法: #!/usr/bin/python str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.spli ...