[LeetCode] 203. Remove Linked List Elements 移除链表元素
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:
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- public class Solution {
- 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;
- }
- }
Java:
- public ListNode removeElements(ListNode head, int val) {
- if(head == null) return head;
- if(head.val == val) return removeElements(head.next, val);
- ListNode preMark = head, nextMark = head;
- while(nextMark.next != null && nextMark.next.val != val){
- nextMark = nextMark.next;
- }
- // This line could be deleted, i kept it here for a full logic cycle.
- if(nextMark.next == null) return head;
- preMark = nextMark;
- nextMark = nextMark.next;
- preMark.next = removeElements(nextMark, val);
- return head;
- }
Python:
- class Solution(object):
- def removeElements(self, head, val):
- """
- :type head: ListNode
- :type val: int
- :rtype: ListNode
- """
- if not head:
- return None
- head.next = self.removeElements(head.next, val)
- return head.next if head.val == val else head
Python: wo from G
- class Solution(object):
- def removeElements(self, head, val):
- """
- :type head: ListNode
- :type val: int
- :rtype: ListNode
- """
- while head and head.val == val:
- head = head.next
- if head:
- head.next = self.removeElements(head.next, val)
- return head
Python:
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, x):
- # self.val = x
- # self.next = None
- class Solution:
- # @param {ListNode} head
- # @param {integer} val
- # @return {ListNode}
- def removeElements(self, head, val):
- dummy = ListNode(float("-inf"))
- dummy.next = head
- prev, curr = dummy, dummy.next
- while curr:
- if curr.val == val:
- prev.next = curr.next
- else:
- prev = curr
- curr = curr.next
- return dummy.next
Python:
- def removeElements(self, head, val):
- dummy = ListNode(-1)
- dummy.next = head
- pointer = dummy
- while(pointer.next):
- if pointer.next.val == val:
- pointer.next = pointer.next.next
- else:
- pointer = pointer.next
- return dummy.next
C++: Iterration
- class Solution {
- public:
- ListNode* removeElements(ListNode* head, int val) {
- ListNode *dummy = new ListNode(-1), *pre = dummy;
- dummy->next = head;
- while (pre->next) {
- if (pre->next->val == val) {
- ListNode *t = pre->next;
- pre->next = t->next;
- t->next = NULL;
- delete t;
- } else {
- pre = pre->next;
- }
- }
- return dummy->next;
- }
- };
C++: Recursion
- class Solution {
- public:
- ListNode* removeElements(ListNode* head, int val) {
- if (!head) return NULL;
- head->next = removeElements(head->next, val);
- return head->val == val ? head->next : head;
- }
- };
All LeetCode Questions List 题目汇总
[LeetCode] 203. Remove Linked List Elements 移除链表元素的更多相关文章
- LeetCode 203. Remove Linked List Elements 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LintCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Have you met this question i ...
- LeetCode OJ :Remove Linked List Elements (移除链表元素)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 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题要求 ...
- LeetCode 203. Remove Linked List Elements (移除链表中的项)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 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 -- ...
- (easy)LeetCode 203.Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- Java [Leetcode 203]Remove Linked List Elements
题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...
随机推荐
- SQL进阶系列之6用关联子查询比较行与行
写在前面 使用SQL对同一行数据进行列间的比较很简单,只需要在WHERE子句里写上比较条件就可以了,对于不同行数据进行列间比较需要使用自关联子查询. 增长.减少.维持现状 需要用到行间比较的经典场景是 ...
- selenium常用的API(四)设置get方法最大加载时间
我们在进行自动化测试的时候,使用get方法打开页面时会等到页面完全加载完才会执行后续操作, 有时我们需要的元素已加载完成,而部分JS未加载完导致加载时间很长,这无疑增加了自动化测试的时间, 针对此情况 ...
- unicode转换为中文
unicode转换为中文 \u5f53\u5730\u65f6\u95f42019\u5e747\u670813\u65e5\uff0c\u82f1\u56fd\u8d1d\u5fb7\u798f\u ...
- sql中如何获取一条数据中所有字段的名称和值
declare ) ) --获取表的列名 ,),filename INTO #templist FROM (select cl.name as filename from sys.tables AS ...
- *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)\),显然无 ...
- 《exception》第九次团队作业:Beta冲刺与验收准备(第二天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...
- monkey内存泄露
monkey内存泄露 1.内存泄露发现方法 执行monkey测试时有时会有内存泄露情况,可以进行排查 a.手机弹出out of memory对话框--基本确定有内存泄露 b.手机没有提示但是实际有内 ...
- HDU - 5571 :tree (动态点分治 异或)
题意:给定一棵树,有点权a[],有边权. 现在有M次修改点权的操作,输出每次修改后,Σ(a[i]^a[j])*dis(i,j); 思路:因为待修改,我们需要快速得到以及修改一个点到其他所有点的信息. ...
- [Codeforces 1242C]Sum Balance
Description 题库链接 给你 \(k\) 个盒子,第 \(i\) 个盒子中有 \(n_i\) 个数,第 \(j\) 个数为 \(x_{i,j}\).现在让你进行 \(k\) 次操作,第 \( ...
- 数据库 Hash Join的定义,原理,算法,成本,模式和位图
Hash Join只能用于相等连接,且只能在CBO优化器模式下.相对于nested loop join,hash join更适合处理大型结果集 Hash Join的执行计划第1个是hash ...