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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

这道移除链表元素是链表的基本操作之一,没有太大的难度,就是考察了基本的链表遍历和设置指针的知识点,我们只需定义几个辅助指针,然后遍历原链表,遇到与给定值相同的元素,将该元素的前后连个节点连接起来,然后删除该元素即可,要注意的是还是需要在链表开头加上一个dummy node,具体实现参见代码如下:

解法一:

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

如果只是为了通过OJ,不用写的那么严格的话,下面这种方法更加简洁,当判断下一个结点的值跟给定值相同的话,直接跳过下一个结点,将next指向下下一个结点,而根本不断开下一个结点的next,更不用删除下一个结点了。最后还要验证头结点是否需要删除,要的话直接返回下一个结点,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. ListNode* removeElements(ListNode* head, int val) {
  4. if (!head) return NULL;
  5. ListNode *cur = head;
  6. while (cur->next) {
  7. if (cur->next->val == val) cur->next = cur->next->next;
  8. else cur = cur->next;
  9. }
  10. return head->val == val ? head->next : head;
  11. }
  12. };

我们也可以用递归来解,写法很简洁,通过递归调用到链表末尾,然后回来,需要要删的元素,将链表next指针指向下一个元素即可:

解法三:

  1. class Solution {
  2. public:
  3. ListNode* removeElements(ListNode* head, int val) {
  4. if (!head) return NULL;
  5. head->next = removeElements(head->next, val);
  6. return head->val == val ? head->next : head;
  7. }
  8. };

类似题目:

Remove Element

Delete Node in a Linked List

参考资料:

https://leetcode.com/problems/remove-linked-list-elements/

https://leetcode.com/problems/remove-linked-list-elements/discuss/57324/AC-Java-solution

https://leetcode.com/problems/remove-linked-list-elements/discuss/57306/3-line-recursive-solution

https://leetcode.com/problems/remove-linked-list-elements/discuss/57331/Accepted-7-line-clean-java-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

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

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

  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] Remove Linked List Elements

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

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

  8. 【LeetCode】203. Remove Linked List Elements

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

  9. 203. Remove Linked List Elements - LeetCode

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

随机推荐

  1. 【转】linux内核中writesb(), writesw(), writesl() 宏函数

    writesb(), writesw(), writesl() 宏函数 功能 : writesb()    I/O 上写入 8 位数据流数据 (1字节) writesw()   I/O  上写入 16 ...

  2. C#开发微信门户及应用(4)--关注用户列表及详细信息管理

    在上个月的对C#开发微信门户及应用做了介绍,写过了几篇的随笔进行分享,由于时间关系,间隔了一段时间没有继续写这个系列的博客了,并不是对这个方面停止了研究,而是继续深入探索这方面的技术,为了更好的应用起 ...

  3. 转 threejs中3D视野的缩放实现

    Threejs基础部分学习知道透视相机new THREE.PerspectiveCamera(fov, aspect , near,far)中. fov视野角(拍摄距离)越大,场景中的物体越小.fov ...

  4. Css3新特性总结之边框与背景(一)

    本系列主要总结Css3一些新特性的认识,来源于<css揭秘>书. 一.半透明边框 css3最好用hsla,而不是rgba,hsla是:h:颜色值(0~360):s:饱合度(0%~100%) ...

  5. css_03之文本、浮动

    1.字体属性:①字体格式:font-family:取值:“microsoft yahei”/Arial……:②字体大小:font-size:取值:pt/px:③字体加粗:font-weight:取值: ...

  6. 金融行业的BI应用分析

    商业智能是一种提高企业智能化的手段,它可以满足企业发展的需要.提高企业的竞争力.同时,对于提高金融行业的风险管理.提升对外服务的质量都能够起到关键性的作用. 在市场竞争和银行业务转型期间,商业智能对于 ...

  7. HTML学习(零)简介

    一)HTML介绍 它是一个超文本标记语言,静态页面. 所谓的'超文本'就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 主要的结构为包括"头"部分(英语:Head).和 ...

  8. 服务器开启https协议

    开启Tomcat https服务 发布企业级应用的时候遇到一个问题,就是IOS7.1之后app的下载地址URL必须是https开头的协议,所以服务器必须支持https协议. 实验环境:Mac OSX ...

  9. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

  10. JavaEE开发基础

    1 JavaEE简介 Java平台有三个版本,分别是JavaSE(Java Platform, Standard Edition),JavaEE(Java Platform, Enterprise E ...