203. Remove Linked List Elements【easy】

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.

解法一:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == NULL) {
return head;
} ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head;
head = dummy; while (head != NULL && head->next != NULL) {
if (head->next->val == val) {
while (head->next != NULL && head->next->val == val) {
ListNode * temp = head->next;
free(temp);
head->next = head->next->next;
}
}
else {
head = head->next;
}
} return dummy->next;
}
};

里面的while可以不用,因为这个题和(82. Remove Duplicates from Sorted List II)不一样,那个题你根本不知道val是什么,所以只用一个if是不行的。但是这个题你知道val是什么,所以可以省略里面的while。

解法二:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == NULL) {
return head;
} ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head;
head = dummy; while (head != NULL && head->next != NULL) {
if (head->next->val == val) {
ListNode * temp = head->next;
free(temp);
head->next = head->next->next;
}
else {
head = head->next;
}
} return dummy->next;
}
};

精简写法

解法三:

 public ListNode removeElements(ListNode head, int val) {
if (head == null) return null;
head.next = removeElements(head.next, val);
return head.val == val ? head.next : head;
}

递归,参考了@renzid 的代码

 

203. Remove Linked List Elements【easy】的更多相关文章

  1. 203. Remove Linked List Elements【Easy】【未排序链表删除其中的给定值】

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

  2. 452. Remove Linked List Elements【Naive】

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

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

  4. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  5. 203. Remove Linked List Elements - LeetCode

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

  6. 【LeetCode】203. Remove Linked List Elements

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

  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】Remove Linked List Elements(easy)

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

  9. 【一天一道Leetcode】#203.Remove Linked List Elements

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. POJ 2348 Euclid's Game(博弈论)

    [题目链接] http://poj.org/problem?id=2348 [题目大意] 给出两个数,两个参赛者轮流用一个数减去另一个数的倍数,当一个数为0的时候游戏获胜, 求先手是否必胜 [题解] ...

  2. Scala实战高手****第14课:Scala集合上的函数式编程实战及Spark源码鉴赏

    package com.dt.spark.scala.bascis object Functional_Itearal {   def main(args: Array[String]): Unit ...

  3. NSNotificationCenter监听TextField文字变化

    注册 1: NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", na ...

  4. iOS开发——使用Autolayout弹出键盘

    参考: http://segmentfault.com/q/1010000002420050 http://blog.csdn.net/qq448631961/article/details/4034 ...

  5. 文件描述符与socket连接

    每个进程开启一个soeket连接,都会占用一个文件描述符.   1. 概述       在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件.       文件 ...

  6. Error: Top-level design entity "dff" is undefined

    原因是:在quartus库文件里面已将dff定义了,要是找使用这个名字重命名了,因而需要重新命名为其他的名字.

  7. CSS3:3D转换

    几个突破口:(为了更简洁理解,先忽略兼容) 1.认识3D的坐标系 rotateX()-----------元素绕X轴旋转 rotateY() -----------元素绕Y轴旋转 rotateZ() ...

  8. easyui-validatebox 的简单长度验证

    验证: 页面代码: <form id="invoiceEdit"> <input id="fpdm" name="fpdm" ...

  9. 基于3D Vision眼镜的OSG立体显示 【转】

    http://blog.csdn.net/qq_20038925/article/details/50510565 OSG 立体显示 3D Vision眼镜:所实现的是被动立体. 1.本人最近在做os ...

  10. win 8 远程桌面文件复制问题(图)

    用win7连接远程桌面.能够非常方便的在宿主机之间文件复制粘贴. 但用win8.1远程连接桌面时,却发现不能拷贝文件了.查看网上资料,最后总结实现此步骤例如以下: win+R,执行mstsc.例如以下 ...