leetcode203. 移除链表元素

leetcode链接

题目

在这里插入图片描述

思路

可以设置一个虚拟头结点,这样原链表的所有节点就都可以按照统一的方式进行移除。

代码

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int):
        new_head = ListNode(val=0,next=head)
        current = new_head
        while current.next:
            if current.next.val == val:  # 不能使用while,末尾为val时有bug
                current.next = current.next.next
            else:
                current = current.next
        return new_head.next

相关推荐

  1. LeetCode203. 元素

    2024-02-07 06:42:02       56 阅读
  2. leetcode203. 元素

    2024-02-07 06:42:02       59 阅读
  3. Leetcode 203 元素

    2024-02-07 06:42:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 06:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 06:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 06:42:02       87 阅读
  4. Python语言-面向对象

    2024-02-07 06:42:02       96 阅读

热门阅读

  1. 学习笔记:正则表达式

    2024-02-07 06:42:02       38 阅读
  2. 安卓开发面试题

    2024-02-07 06:42:02       46 阅读
  3. Unreal Engine 中的插值方法示例

    2024-02-07 06:42:02       44 阅读
  4. 关联语句join与合并语句union

    2024-02-07 06:42:02       44 阅读
  5. Linux系统防火墙设置

    2024-02-07 06:42:02       50 阅读
  6. apache_exporter安装说明

    2024-02-07 06:42:02       42 阅读
  7. [C++] sqlite3_get_table 的使用

    2024-02-07 06:42:02       45 阅读
  8. 跟着GPT学设计模式之原型模式

    2024-02-07 06:42:02       56 阅读
  9. 如何使用postman进行接口调试

    2024-02-07 06:42:02       55 阅读
  10. 【wu-easy-excel-starter】1.2.2-JDK17-SNAPSHOT

    2024-02-07 06:42:02       46 阅读