Python | Leetcode Python题解之第203题移除链表元素

题目:

题解:

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        dummy_head = ListNode(next=head) #添加一个虚拟节点
        cur = dummy_head
        while(cur.next!=None):
            if(cur.next.val == val):
                cur.next = cur.next.next #删除cur.next节点
            else:
                cur = cur.next
        return dummy_head.next
相关推荐
for_ever_love__9 分钟前
python基础语法学习: requirements.txt
开发语言·python·学习
程序员清风11 分钟前
FastAPI 入门:用 Python 快速开发现代后端服务
python·oracle·fastapi
SunnyDays101120 分钟前
使用 Python 提取 Word 文档中的表格数据
python·word
for_ever_love__21 分钟前
python基础语法学习: 动态类型
开发语言·python·学习
troy1281 小时前
分布式系统框架选型指南:主流框架优缺点深度对比
python·django·pygame
129Lab1 小时前
BMS算法快速原型开发:AI辅助实现扩展卡尔曼滤波(EKF)SOC估算从理论到代码落地
python·嵌入式开发·bms·卡尔曼滤波·电池管理系统·soc估算
萧鼎1 小时前
safetensors 库的安装、核心语法与实战用法,安全保存模型权重
python·开源·教程·python库·safetensors
千里码aicood1 小时前
基于Python语言的测量程序设计
开发语言·python
2601_962300811 小时前
用PyTorch Lightning简化空间分析:Python和机器学习的力量
python·深度学习·机器学习·空间分析·pytorchlightning
jayson.h2 小时前
python-可视化提取表格-通用
开发语言·python