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
相关推荐
ValhallaCoder2 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
猫头虎3 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
八零后琐话4 小时前
干货:程序员必备性能分析工具——Arthas火焰图
开发语言·python
月挽清风4 小时前
代码随想录第十五天
数据结构·算法·leetcode
青春不朽5125 小时前
Scrapy框架入门指南
python·scrapy
MZ_ZXD0015 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
TracyCoder1236 小时前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode
全栈老石6 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
梨落秋霜6 小时前
Python入门篇【模块/包】
python