题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/

题目描述:

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

思路:

迭代

class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(0)
dummy.next = head
prev = dummy
last = prev.next
while last :
if last.val == val:
prev.next = last.next
last = prev.next
else:
prev = prev.next
last = prev.next
return dummy.next

递归

class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
if not head: return
head.next = self.removeElements(head.next, val)
return head.next if head.val == val else head

[LeetCode] 203. 移除链表元素的更多相关文章

  1. Java实现 LeetCode 203 移除链表元素

    203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...

  2. [LeetCode] 203. 移除链表元素(链表基本操作-删除)、876. 链表的中间结点(链表基本操作-找中间结点)

    题目 203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 题解 删除结点:要注意虚拟头节点. 代码 class Solution { public ListNode removeEle ...

  3. 【LeetCode】203.移除链表元素

    203.移除链表元素 知识点:链表:双指针 题目描述 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 . 示例 ...

  4. 力扣(LeetCode)移除链表元素 个人题解

    删除链表中等于给定值 val 的所有节点. 这题粗看并不困难,链表的特性让移除元素特别轻松,只用遇到和val相同的就跳过,将指针指向下一个,以此类推. 但是,一个比较麻烦的问题是,当链表所有元素都和v ...

  5. LeetCode 203——移除链表(JAVA)

    删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2->3->4 ...

  6. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

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

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

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

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

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

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

随机推荐

  1. aspose 模板输出

    Dictionary<string, string> dictionnaryBig = new Dictionary<string, string>(); dictionnar ...

  2. html body标签 语法

    html body标签 语法 标签body是什么意思? 标签body是一个网页的身体部分,也就是用于定义网页的主体内容,也是一个HTML文档中必须的部分. 作用:定义文档的主体. 广州大理石机械构件 ...

  3. vue 使用 axios 时 post 请求方法传参无法发送至后台

    axios 时 post 请求方法传参无法发送至后台报错如下 Response to preflight request doesn't pass access control check: No ' ...

  4. xshell的快捷键

    https://blog.csdn.net/hellozpc/article/details/46753575

  5. wannafly 挑战赛8 E 小G的项链(manecher)

    链接:https://www.nowcoder.com/acm/contest/57/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...

  6. Django REST framework的解析器与渲染器

    解析器 解析器的作用 解析器的作用就是服务端接收客户端传过来的数据,把数据解析成自己可以处理的数据.本质就是对请求体中的数据进行解析. 在了解解析器之前,我们要先知道Accept以及ContentTy ...

  7. 每日踩坑 2019-08-23 button 元素点击后刷新页面

    button标签按钮会提交表单. 解决方案: <button class="btn btn-primary" type="button" id=" ...

  8. jQuery.parseJSON()

    https://api.jquery.com/jQuery.parseJSON/ https://api.jquery.com/category/deprecated/deprecated-3.0/ ...

  9. HashMap,ConcurrentHashMap相关知识整理

    1.HashMap的存储步骤: 1.传入key和value,判断key是否为null,如果为null,则调用putForNullKey,以null作为key存储到哈希表中: 2. 然后计算key的ha ...

  10. Python 使用Qt进行开发(三)

    下面我们实现日期时间框的添加,表示日期时间的文本框可以使用QtWidgets控件下的 QDateEdit() , QTimeEdit() , QDateTime() 三个方法实现. 1,使用QDate ...