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

Have you met this question in a real interview?

 
 
Example

Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

LeetCode上的原题,请参见我之前的博客Remove Linked List Elements

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param head a ListNode
  5. * @param val an integer
  6. * @return a ListNode
  7. */
  8. ListNode *removeElements(ListNode *head, int val) {
  9. ListNode *dummy = new ListNode(-), *pre = dummy;
  10. dummy->next = head;
  11. while (pre->next) {
  12. if (pre->next->val == val) {
  13. ListNode *t = pre->next;
  14. pre->next = t->next;
  15. t->next = NULL;
  16. } else {
  17. pre = pre->next;
  18. }
  19. }
  20. return dummy->next;
  21. }
  22. };

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param head a ListNode
  5. * @param val an integer
  6. * @return a ListNode
  7. */
  8. ListNode *removeElements(ListNode *head, int val) {
  9. if (!head) return NULL;
  10. head->next = removeElements(head->next, val);
  11. return head->val == val ? head->next : head;
  12. }
  13. };

[LintCode] Remove Linked List Elements 移除链表元素的更多相关文章

  1. [LeetCode] Remove Linked List Elements 移除链表元素

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

  2. [LeetCode] 203. Remove Linked List Elements 移除链表元素

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

  3. LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java

    Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...

  4. LeetCode OJ :Remove Linked List Elements (移除链表元素)

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

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

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

  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--LinkedList--203. Remove Linked List Elements(Easy)

    203. Remove Linked List Elements(Easy) 题目地址https://leetcode.com/problems/remove-linked-list-elements ...

  9. 203. Remove Linked List Elements - LeetCode

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

随机推荐

  1. JVM内存结构、垃圾回收那点事

    翻看电脑的文件夹,无意看到了9月份在公司做的一次分享,浏览了一下"婆婆特",发现自己在ppt上的写的引导性问题自己也不能确切的回答出来,哎,知识这东西,平时不常用的没些日子就生疏了 ...

  2. MongoDB3.0新特性

    3月3日,MongoDB3.0终于发布了. 主要特点包括了对之前收购的WiredTiger存储引擎的支持,插件式存储引擎API,SCRAM-SHA-1认证机制,并改进了解释功能.此外,包含了自动化.备 ...

  3. Hibernate一对一映射关联

    Hibernate提供了两种一对一映射关联关系的方式: 1)按照外键映射 2)按照主键映射 下面以员工账号表和员工档案表(员工账号和档案表之间是一对一的关系)为例,介绍这两种映射关系,并使用这两种 映 ...

  4. 【转】html、css、js文件加载顺序及执行情况

    原链接:http://www.cnblogs.com/Walker-lyl/p/5262075.html 今天看书,看到html,css,js加载执行情况,发现自己并不是真正的了解,网上搜了半小时依然 ...

  5. BZOJ 2648 SJY摆棋子 ——KD-Tree

    [题目分析] KD-Tree第一题,其实大概就是搜索剪枝的思想,在随机数据下可以表现的非常好NlogN,但是特殊数据下会达到N^2. 精髓就在于估价函数get以及按照不同维度顺序划分的思想. [代码] ...

  6. PHP 批量修改图片的名字

    <?php // glob() 返回指定目录下的文件名以及目录 $arr = glob("img/*.jpg"); $time = time(); $i = 100001; ...

  7. js-js实现,在HTML中使用JavaScript,基本概念

    Js实现: 1.JavaScript实现的组成: 核心(ECMAScript):由ECMA-262定义,提供核心语言功能 文档对象模型(DOM)提供访问和操作网页内容的方法以及接口 浏览器对象模型(B ...

  8. delphi 时间格式操作

    FormatDateTime('yyyy-mm-dd hh:nn:ss',Now) FormatDateTime('hh:mm:ss:zz',Now) if (TimeOf(now) < pub ...

  9. JavaScript 笔记 ( Prototype )

    这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...

  10. UVa1161 Objective: Berlin(最大流)

    题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...