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 移除链表元素的更多相关文章

  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. PAT甲级1008水题飘过

    题目分析:上去下来到达的时间和数量 #include<iostream> using namespace std; ]; int main(){ int n; while(scanf(&q ...

  2. 使用Xpath爬虫库下载诗词名句网的史书典籍类所有文章。

    # 需要的库 from lxml import etree import requests # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows ...

  3. jpa之No property buyerOpenId found for type OrderMaster! Did you mean 'buyerOpenid'?

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  4. keras神经网络开发知识笔记

    mnist数据集获取60000个训练样本和10000个测试样本,样本为0-9十个数字,用全连接神经网络进行训练,并测试结果. 例程采用60000个数据样本进行训练,对于一般的电脑来说,这样训练会比较费 ...

  5. test20190926 孙耀峰

    70+100+0=170.结论题自己还是要多试几组小数据.这套题还不错. ZYB建围墙 ZYB之国是特殊的六边形构造. 已知王国一共有

  6. 一个Tomcat下部署多个项目异常:org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean 的解决方法

    内容简介 在测试服务器上Tomcat下部署两个Spring boot项目,总是一个能启动成功,另一个启动不成功.这两个war包单独部署均能正常启动. 查看日志:启动时报出 org.springfram ...

  7. php 正则表达示中的原子

    原子 原子是正则表达示里面的最小单位,原子说白了就是需要匹配的内容.一个成立的正则表达示当中必须最少要有一个原子.大理石平台精度等级 所有可见不可见的字符就是原子 说明:我们见到的空格.回车.换行.0 ...

  8. 洛谷 P1226 【模板】快速幂||取余运算 题解

    Analysis 快速幂模板,注意在最后输出时也要取模. 快速幂模板 inline ll ksm(ll x,ll y) { ll ans=; ) { ) { ans*=x; ans%=k; } x*= ...

  9. learning scala akka tell pattern(二)

    package com.example import akka.actor._ object Tutorial_02_Tell_Pattern extends App { println(" ...

  10. GitHub 远程仓库 de 第一次配置

    GitHub远程仓库, Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.首先找一台电脑充当服务器的角色, 每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上 ...