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. BZOJ 1878 [SDOI2009]HH的项链(扫描线+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1878 [题目大意] 给出一个数列,给出m个查询,每次查询一个区间中不相同的数字个数 [ ...

  2. [BZOJ1513]Tet-Tetris 3D

    get了新的标记永久化技能- 这题要求询问max和覆盖,因为是线段树套线段树,所以内外都不可以标记下传 这种标记永久化的套路是维护两个标记:$mx,all$,$mx$表示这个子树内的真最大值,$all ...

  3. 【数论】【欧拉函数】【筛法求素数】【乘法逆元】【快速幂取模】bzoj2186 [Sdoi2008]沙拉公主的困惑

    http://www.cnblogs.com/BLADEVIL/p/3490321.html http://www.cnblogs.com/zyfzyf/p/3997986.html 翻了翻题解,这两 ...

  4. java 面向接口编程的理解

    初学者可能在学习中会有很多疑惑,为什么要这样,明明可以那样实现,这样做的好处又是什么? 可能会的人觉得很简单很容易理解,甚至可能觉得问的问题很智障,但对于小白来说可能是苦思冥想都不得其解的. 自己身为 ...

  5. 动态RIP配置路由表

    动态RIP配置路由表 以Router11为例子: (1)配置端口ip(两个端口需要设置两个ip) Router(config)#inter f0/0 Router(config-if)#ip add ...

  6. [读书笔记] 你早该这么玩Excel

    <你早该这么玩Excel>只教你做两件事:如何设计一张“天下第一表”,你会恍然大悟,以前遇到的种种麻烦是因为做错了表格:如何一分钟“变”出N张表,你会明白表格是“变”出来的,不是“做”出来 ...

  7. P2P通信标准协议(二)之TURN

    上一篇P2P通信标准协议(一)介绍了在NAT上进行端口绑定的通用规则,应用程序可以根据这个协议来设计网络以外的通信. 但是,STUN/RFC5389协议里能处理的也只有市面上大多数的Cone NAT( ...

  8. nhibernate change connection

    http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring http://stackoverflow ...

  9. Kubernetes dashboard集成heapster

    图形化展示度量指标的实现需要集成k8s的另外一个Addons组件: Heapster . Heapster原生支持K8s(v1.0.6及以后版本)和 CoreOS ,并且支持多种存储后端,比如: In ...

  10. centos6.9安装xampp后报错:egrep: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

    1.centos6.9安装xampp(xampp-linux-x64-7.0.21-0-installer.run)后启动的时候,报错: egrep: error while loading shar ...