[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 --> ...
随机推荐
- mac 使用 brew 安装 nginx 及各种命令
一.安装 brew install nginx 或 sudo brew install nginx 二.启动 brew services start nginx 或 sudo brew service ...
- APP测试之MONKEY安装、使用
1.先下载java的jdk;配置java变量 安装好之后会有两个文件夹一个是jdk 一个是jre(运行)然后配置好java环境变量:JAVA_HOME:C:\Program Files\Java\jd ...
- 微信小程序~扫码
为了让用户减少输入,我们可以把复杂的信息编码成一个二维码,利用宿主环境wx.scanCode这个API调起微信扫一扫,用户扫码之后,wx.scanCode的success回调会收到这个二维码所对应的字 ...
- robot framework 笔记(三),RF安装
背景: 本来robot framework的安装应该放在一开始写的,因写博客的时候已经装过了,恰巧重装系统又重装了一遍RF RF推荐使用python2, 使用3的话会遇到一些页面非友好的问题 需要的安 ...
- sort()函数中的key
d = { , , } #for k in d.items(): # print(k) content = list(d.items()) print(content) content.sort(ke ...
- pass的作用?
1.空语句 do nothing 2.保证格式完整,保证语义完整 3.占位语句
- Cramer-Rao Bounds (CRB)
克拉美-罗界.又称Cramer-Rao lower bounds(CRLB),克拉美-罗下界. 克拉美罗界是对于参数估计问题提出的,为任何无偏估计量的方差确定了一个下限.无偏估计量的方差只能无限制的逼 ...
- MySQL 内连接、外连接、左连接、右连接、全连接……太多了
用两个表(a_table.b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接.外连接( 左(外)连接.右(外)连接.全(外)连接). 主题:内连接 ...
- vigil rpm 包制作
vigil 可以方便的进行服务的监控,以下尝试制作一个rpm 包,方便使用 安装依赖 ruby yum -y install ruby rubygems ruby-devel 修改gem 源 可选, ...
- 关于java项目跑着跑着就挂掉的问题
部署项目后,安装redis,从redis中获取数据,或一些数据库查询操作,服务器cpu和内存占用率突增.