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. js的MVC结构设计

    基于jquery的Deferred,设计出如下MVC架构. 模型model interface.js interface: function(userid){ var dtd = $.Deferred ...

  2. Python - BeautifulSoup 安装

    BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...

  3. Couchbase用的端口

    文档首页: http://www.couchbase.com/documentation http://docs.couchbase.com/couchbase-manual-2.2/#prepara ...

  4. producer怎样发送消息到指定的partitions

    http://www.aboutyun.com/thread-9906-1-1.html http://my.oschina.net/u/591402/blog/152837 https://gith ...

  5. 实现正在加载中界面的Android库:DynamicBox

    转载. DynamicBox是一个Android库,能够inflates自定义布局来指示出: 正在加载内容 显示一个异常 或者是一个自定义视图     项目主页:http://www.open-ope ...

  6. asp.net手动填充TreeView生成树

    最近在做项目发现需要用到树的地方,页面的前台任然是使用一个asp.net的控件TreeView来显示树的结构,当然也可以自己在前台写一个树来展示,这在后期跟局功能的不同很大可能会要用到异步的知识,废话 ...

  7. SQL 优化,全

    性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的SQL语句,要设 ...

  8. 【转】 UIView如何管理它的子视图

    原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...

  9. jQuery实现的向下推送图文信息滚动效果

    HTML 我们以新浪微博信息滚动为背景,html中包含了多条微博图文信息,结构如下: <div id="con"> <ul> <li> < ...

  10. WHU 1579 Big data (DP)

    题意: f[0]=0,f[i]=f[i-1]+a or b. 求满足L<=∑f[n]<=R的序列的种数 n<100.  |a|,|b|<=10000.  |L|,|R|< ...