[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 --> ...
随机推荐
- centos6.5安装crmsh
CentOS默认没有crmsh的yum源,因此可以借用OpenSUSE的源(OpenSUSE的包也是rpm). 操作步骤很简单首先先进入yum源的安装目录,下载repo配置文件,(返回原工作目录,)执 ...
- 《Java程序设计实验》 软件工程18-1,3 OO实验2
- SQL中and和or的区别是?
今天有这样得一个需求,如果登陆人是客服的话,会查询订单是’该客服’以及还没有匹配客服的,刚开始想的是直接在sql语句上拼写 or assigned_id is null 的,测试了一下发现这样的 ...
- Centos7-新增硬盘挂载
查看现有硬盘情况 df -h fdisk -l 查看新硬盘 ls /dev/sdb 具体操作 fdisk /dev/sdb m n #添加一个新的分区 p #创建主分区 w #保存并退出 partpr ...
- test20190924 老L
80+50+100=230.T1没做出来说明我数列学得不好? LOLO 的含树 现有函数 \[ g_m(i)=\begin{cases} 0, & 0 \leq i \leq m\\ i-1+ ...
- Flume架构以及应用介绍(转)
在具体介绍本文内容之前,先给大家看一下Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步,从而引出 ...
- 使用postman上传excel文件测试导入excel
今日思语:城市的生活很快,有时学会让自己慢下来,慢慢来 对于做一些文件上传操作时,一般我们是直接在前端页面加入类型为file的input标签,也可以使用postman来进行文件的上传测试,如下: po ...
- CDN工作机制
CDN(content delivery network),即内容分布网络,是一种构建在现有Internet上的一种先进的流量分配网络.CDN以缓存网站中的静态数据为主,当用户请求动态内容时,先从CD ...
- C# list常用的几个操作 改变list中某个元素的值 替换某一段数据
1.改变list中某个元素的值 public class tb_SensorRecordModel { public int ID { get; set; } public decimal Value ...
- nginx重启 平滑重启
进入 ngiinx sbin目录下./nginx -c /usr/local/nginx/conf/nginx.conf -c参数指定了要加载的nginx配置文件路径 停止操作停止操作是通过向ngin ...