[LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst
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的更多相关文章
- 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 --& ...
- [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 --> ...
- [LeetCode] 203. 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 移除链表元素 C++/Java
Remove all elements from a linked list of integers that have value val. Example: Input: ->->-& ...
- [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 ...
随机推荐
- Git学习之Git检出
================================================ HEAD 的重置即检出 ======================================= ...
- 【分享】Linux(Ubuntu)下如何自己编译JDK
最近在看<深入理解 Java 虚拟机>这本书.里面提到了如何手动编译JDK,于是就试了试. 在编译的过程中,遇到了一些问题.上网一搜,发现了一篇很好的文章,跟大家分享一下:ubuntu 1 ...
- DIV高度自适应及注意问题(转)
本文和大家重点讨论一下DIV高度自适应及注意问题,主要包括父div高度随子div的高度改变而改变和子div高度随父亲div高度改变而改变两种情况. DIV高度自适应及注意问题 积累了一些经验,总结出一 ...
- 关于ASP.NET和.NET的区别和联系
对于一个新手,往往会被这些名字给搞蒙了,对不起(笨小孩我也被搞蒙过,见笑啦),这归根结底还是怪自己对知识掌握和了解的不够,废话不多,直接到主题. ASP.NET和.NET的区别和联系 .NET 一般所 ...
- CSS+transform画动态表情
先给大家看下画完后是什么样子: 代码看这里: html代码: <body> <div class="emoji emoji_like"> <div c ...
- sencha touch list ListPaging使用详解
示例代码: Ext.define('app.view.message.List', { alternateClassName: 'messageList', extend: 'Ext.List', x ...
- 常用linux命令及shell脚本
参考:Linux命令大全 分割大文件 Split命令 按行分割(只能是文本文件) $split -l 1000 big_file 前缀 按文件大小分割 $split -b 64m big_file 前 ...
- 【题目】求n以内的素数个数
最近在leetCode上刷提,还是满锻炼人的,为以后面试打基础吧.不多说下面开始. 问题:求[2,n]之间的素数的个数. 来源:leetCode OJ 提示: Let's start with a i ...
- 用AT命令调试调制解调器
最早生产调制解调器的公司是贺氏,后来组建的厂家制造的调制解调器都与HAYES兼容,大部分的通信软件使用菜单来对调制解调器进行配置.检测.但是有些通信软件要求用户直接发命令给调制解调器,在这种情况下必须 ...
- iOS 通过(lame)将录制音频转换成Mp3
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处,保留原帖地址及作者署名. Url:http://blog.csdn.net/ysy441088327/article/detail ...