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.

解法一:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* removeElements(ListNode* head, int val) {
  12. if (head == NULL) {
  13. return head;
  14. }
  15.  
  16. ListNode * dummy = new ListNode(INT_MIN);
  17. dummy->next = head;
  18. head = dummy;
  19.  
  20. while (head != NULL && head->next != NULL) {
  21. if (head->next->val == val) {
  22. while (head->next != NULL && head->next->val == val) {
  23. ListNode * temp = head->next;
  24. free(temp);
  25. head->next = head->next->next;
  26. }
  27. }
  28. else {
  29. head = head->next;
  30. }
  31. }
  32.  
  33. return dummy->next;
  34. }
  35. };

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

解法二:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* removeElements(ListNode* head, int val) {
  12. if (head == NULL) {
  13. return head;
  14. }
  15.  
  16. ListNode * dummy = new ListNode(INT_MIN);
  17. dummy->next = head;
  18. head = dummy;
  19.  
  20. while (head != NULL && head->next != NULL) {
  21. if (head->next->val == val) {
  22. ListNode * temp = head->next;
  23. free(temp);
  24. head->next = head->next->next;
  25. }
  26. else {
  27. head = head->next;
  28. }
  29. }
  30.  
  31. return dummy->next;
  32. }
  33. };

精简写法

解法三:

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

递归,参考了@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. newcoder contest 114 B - 求值2

    记得这是一个组合数卷积的板子题. ∑ C(A,i) * C(B,D-i)   =  C(A+B,D) 然后就直接做了. #include<cstdio> #include<cctyp ...

  2. 【找规律】Codeforces Round #392 (Div. 2) C. Unfair Poll

    C. Unfair Poll time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. 【分块】bzoj2120 数颜色

    分块,在每个点记录一下它之前离它最近的相同颜色的位置pre[i],显然问题转化成了求[l,r]中pre[i]<l的值的个数. 这是分块擅长的,在每个块内记录有序表,查询时对零散的暴力,整块的二分 ...

  4. 安装notepad++ in ubuntu16.04

    一.安装notepad++ Ubuntu下的安装方法: sudo add-apt-repository ppa:notepadqq-team/notepadqq sudo apt-get update ...

  5. javascript快速入门9--引用类型

    引用类型通常叫做类(class),也就是说,遇到引用值,所处理的就是对象. 注意:从传统意义上来说,ECMAScript 并不真正具有类.事实上,除了说明不存在类,在 ECMA-262 中根本没有出现 ...

  6. Spark Streaming on Kafka解析和安装实战

    本课分2部分讲解: 第一部分,讲解Kafka的概念.架构和用例场景: 第二部分,讲解Kafka的安装和实战. 由于时间关系,今天的课程只讲到如何用官网的例子验证Kafka的安装是否成功.后续课程会接着 ...

  7. 【Hadoop】HDFS源码解读

    1.open流程 2.get DFS流程: 3.获取block信息流程

  8. kafka启动报错:kafka.common.KafkaException: Failed to acquire lock on file .lock

    kafka 异常退出后重启时遇到的问题 解决: 执行 netstat -lnp|grep 9092 在执行结果中找到进程号执行 kill -9 进程号再尝试启动Kafka  

  9. BIEE-CSS样式大全

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...

  10. 移动负载均衡技术(MBL)

    移动负载均衡技术(MBL)   转至元数据结尾 附件:5 被admin添加,被admin最后更新于四月 27, 2015 转至元数据起始 互联网技术发展到今天,已经进入移动时代,很多在传统CS和BS的 ...