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. [Redis] C#中使用redis

    C#中使用redis 首先打开Visual Studio建立一个简单的控制台应用程序,我这里暂时使用的VS2013的版本. 然后通过Nuget进行安装Redis常用组件ServiceStack.Red ...

  2. eclipse代码注释的设置

    http://blog.csdn.net/shiyuezhong/article/details/8450578 1. eclipse用户名的设置: 在eclipse的安装路径下,打开eclipse. ...

  3. SQL某个字段在原内容上增加固定内容或replace查找替换内容

    今天正好遇到一个SQL小问题,特做备注 在原有的表中数据如pic 在不动原内容的基础上增加../路径,但不能修改原数据值 原数据 SQL: pic字段 需要增加'../'的内容 update Bmps ...

  4. 简单的SqlHelper

    namespace Login { class SqlHelper { //连接数据库的字符串 //static string dataConnection = "server=***-PC ...

  5. 转-SecureCRT设置

    原帖地址:http://www.2cto.com/os/201410/341569.html 一.基本设置 1.修改设置 为了SecureCRT用起来更方便,需要做一些设置,需要修改的有如下几处: 1 ...

  6. ejs 基本语法

    1.基本语法.<% code %>    无缓冲的条件语句元素.<%= code %>    转义HTML,该code并且会打印出来.<%- code %>     ...

  7. underscorejs-shuffle学习

    2.21 shuffle 2.21.1 语法 _.shuffle(list) 2.21.2 说明 返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序. ...

  8. this详解:JAVASCRIPT中的this到底是谁?

    语法 this 全局对象 在全局执行上下文(函数之外),this引用的是全局对象. console.log(this.document === document); // true // In web ...

  9. jQuery幻灯片skitter-slider插件学习总结

    @(关键词)[skitter|jquery|网页幻灯片|slider] Skitter 是一个非常酷炫的jQuery网页幻灯片插件,支持非常多种酷炫幻灯片切换方式,下载前往官网,另有参考文档 下面简单 ...

  10. php基础知识【函数】(3)字符串string

    一.大小写转换 1.strtolower()--转换为小写. echo strtolower("Hello WORLD!"); //hello world! 2.strtouppe ...