Remove all elements from a linked list of integers that have value val.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

基本思路就是直接建一个新的head, 然后每次将非val值的node copy进入新head的tail上面.

比较腻害的做法是用DFS的recursive方法.

Code

1) basic

class Solution:
def remLinkedList(self, head, val):
ans = dummy = ListNode(1)
while head:
if head.val != val:
dummy.next = ListNode(head.val)
dummy = dummy.next
head = head.next
return ans.next

2) recursive

class Solution:
def remLinkedList(self, head, val):
if not head :return
head.next = self.remLinkedList(head.next, val)
return head.next if head.val == val else head

[LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst的更多相关文章

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

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

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

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

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

  4. 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 -- ...

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

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

  6. Java [Leetcode 203]Remove Linked List Elements

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

  7. [LeetCode] 203. Remove Linked List Elements 解题思路

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

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

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

  9. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

随机推荐

  1. 原生js--表单

    阅读了<javascript权威指南>P396-P409. 一.表单和表单元素的选取 1.选取表单(包含name=“address”属性的form表单) document.querySel ...

  2. ogg 12c OGG-01163

    数据同步报错: 2017-07-03 12:44:36 ERROR OGG-01163 Oracle GoldenGate Delivery for Oracle, rora_t1.prm: Bad ...

  3. linux中删除文件名称乱码

    在最近的操作中发现一些上传的文件有乱码,更改几次都无法正常转换到中文.下面给出正确的解决方案: 使用 ls -i 或者 ls -inum 查找出文件id号(红色字体) [root@localhost ...

  4. Word插入htm文件导致文本域动态增加的一个问题

    受html标签影响,超链接也会被变成文本域(HYPERLINK).当遍历文本域进行替换之前如果预存了文本域的数量(Count/Length/etc.)将导致遗漏.

  5. html to openxml

    Html to OpenXml How to start ? Create a new console application. Add a reference to DocumentFormat.O ...

  6. [工具] CintaNotes

    CintaNotes是一款非常轻巧实用的笔记软件,可看作EverNote轻量级替代品.CintaNotes只需1个exe,体积仅1MB,却拥有 EverNote易于收集.实时搜索.条状排列.tag分类 ...

  7. Android - dhroid 开发框架

    extends:http://www.eoeandroid.com/thread-326973-1-1.html 开源中国地址:http://www.oschina.net/p/dhroid 开源项目 ...

  8. python的十进制与任意进制的转换

    将任意进制转换成十进制 ", 8)) # 表示把8进制的54转换成十进制数并输出结果. # 8可以是2.8,10,16等进制数 将十进制转换成任意进制 def f(n,x): #n为待转换的 ...

  9. empty对如下8种情况返回true

    1.strrchr函数 在W3School站点上的注释如下: strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符.如果成失败,否则返回 fals ...

  10. hdu2896 病毒肆虐【AC自动机】

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...