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: 递归

Java:

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode removeElements(ListNode head, int val) {
  11. if (head == null) return null;
  12. head.next = removeElements(head.next, val);
  13. return head.val == val ? head.next : head;
  14. }
  15. }  

Java:

  1. public ListNode removeElements(ListNode head, int val) {
  2. if(head == null) return head;
  3. if(head.val == val) return removeElements(head.next, val);
  4.  
  5. ListNode preMark = head, nextMark = head;
  6.  
  7. while(nextMark.next != null && nextMark.next.val != val){
  8. nextMark = nextMark.next;
  9. }
  10.  
  11. // This line could be deleted, i kept it here for a full logic cycle.
  12. if(nextMark.next == null) return head;
  13.  
  14. preMark = nextMark;
  15. nextMark = nextMark.next;
  16.  
  17. preMark.next = removeElements(nextMark, val);
  18.  
  19. return head;
  20. }  

Python:

  1. class Solution(object):
  2. def removeElements(self, head, val):
  3. """
  4. :type head: ListNode
  5. :type val: int
  6. :rtype: ListNode
  7. """
  8. if not head:
  9. return None
  10.  
  11. head.next = self.removeElements(head.next, val)
  12.  
  13. return head.next if head.val == val else head  

Python: wo from G

  1. class Solution(object):
  2. def removeElements(self, head, val):
  3. """
  4. :type head: ListNode
  5. :type val: int
  6. :rtype: ListNode
  7. """
  8. while head and head.val == val:
  9. head = head.next
  10.  
  11. if head:
  12. head.next = self.removeElements(head.next, val)
  13.  
  14. return head  

Python:

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution:
  8. # @param {ListNode} head
  9. # @param {integer} val
  10. # @return {ListNode}
  11. def removeElements(self, head, val):
  12. dummy = ListNode(float("-inf"))
  13. dummy.next = head
  14. prev, curr = dummy, dummy.next
  15.  
  16. while curr:
  17. if curr.val == val:
  18. prev.next = curr.next
  19. else:
  20. prev = curr
  21.  
  22. curr = curr.next
  23.  
  24. return dummy.next  

Python:

  1. def removeElements(self, head, val):
  2. dummy = ListNode(-1)
  3. dummy.next = head
  4. pointer = dummy
  5.  
  6. while(pointer.next):
  7.  
  8. if pointer.next.val == val:
  9. pointer.next = pointer.next.next
  10. else:
  11. pointer = pointer.next
  12.  
  13. return dummy.next  

C++: Iterration

  1. class Solution {
  2. public:
  3. ListNode* removeElements(ListNode* head, int val) {
  4. ListNode *dummy = new ListNode(-1), *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. };

C++: Recursion  

  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. };

  

All LeetCode Questions List 题目汇总

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

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

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

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

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

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

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

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

  6. LeetCode 203. Remove Linked List Elements (移除链表中的项)

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

  7. Java for LeetCode 203 Remove Linked List Elements

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

  8. (easy)LeetCode 203.Remove Linked List Elements

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

  9. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

随机推荐

  1. SQL进阶系列之6用关联子查询比较行与行

    写在前面 使用SQL对同一行数据进行列间的比较很简单,只需要在WHERE子句里写上比较条件就可以了,对于不同行数据进行列间比较需要使用自关联子查询. 增长.减少.维持现状 需要用到行间比较的经典场景是 ...

  2. selenium常用的API(四)设置get方法最大加载时间

    我们在进行自动化测试的时候,使用get方法打开页面时会等到页面完全加载完才会执行后续操作, 有时我们需要的元素已加载完成,而部分JS未加载完导致加载时间很长,这无疑增加了自动化测试的时间, 针对此情况 ...

  3. unicode转换为中文

    unicode转换为中文 \u5f53\u5730\u65f6\u95f42019\u5e747\u670813\u65e5\uff0c\u82f1\u56fd\u8d1d\u5fb7\u798f\u ...

  4. sql中如何获取一条数据中所有字段的名称和值

    declare ) ) --获取表的列名 ,),filename INTO #templist FROM (select cl.name as filename from sys.tables AS ...

  5. *P2398 GCD SUM[数论]

    题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 解析 给出n求sum. gcd(x,y)表示x,y的最大公约数. 直接枚举复杂度为\(O(n^2)\),显然无 ...

  6. 《exception》第九次团队作业:Beta冲刺与验收准备(第二天)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...

  7. monkey内存泄露

    monkey内存泄露 1.内存泄露发现方法 执行monkey测试时有时会有内存泄露情况,可以进行排查 a.手机弹出out of  memory对话框--基本确定有内存泄露 b.手机没有提示但是实际有内 ...

  8. HDU - 5571 :tree (动态点分治 异或)

    题意:给定一棵树,有点权a[],有边权. 现在有M次修改点权的操作,输出每次修改后,Σ(a[i]^a[j])*dis(i,j); 思路:因为待修改,我们需要快速得到以及修改一个点到其他所有点的信息. ...

  9. [Codeforces 1242C]Sum Balance

    Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \( ...

  10. 数据库 Hash Join的定义,原理,算法,成本,模式和位图

    Hash Join只能用于相等连接,且只能在CBO优化器模式下.相对于nested loop join,hash join更适合处理大型结果集       Hash Join的执行计划第1个是hash ...