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
相关推荐
qq_18980703几秒前
如何在 Django ListView 中正确过滤当前用户的照片数据
jvm·数据库·python
m0_377618231 分钟前
Go语言如何用systemd_Go语言systemd服务管理教程【总结】
jvm·数据库·python
棉猴5 分钟前
python海龟绘图之计算夹角towards()
开发语言·python·turtle·海龟绘图·towards
星马梦缘11 分钟前
强化学习实战8.1——用PPO打赢星际争霸【环境配置与下位机代码】
人工智能·python·jupyter·强化学习·星际争霸·stablebaseline3·starcraft2
qq_1898070312 分钟前
SQL快速查找分组记录数异常的分类_利用HAVING筛选
jvm·数据库·python
m0_7478545214 分钟前
Python模型保存为ONNX格式_跨平台推理部署与加速技巧
jvm·数据库·python
YuanDaima204814 分钟前
Python 数据结构与语法速查笔记
开发语言·数据结构·人工智能·python·算法
粉嘟小飞妹儿14 分钟前
怎么关闭MongoDB不需要的HTTP管理接口及REST API
jvm·数据库·python
qq_2069013915 分钟前
c++如何将浮点数按指定精度写入文本_setprecision用法【实战】
jvm·数据库·python
2401_8654396318 分钟前
如何管理Oracle服务器的内核共享内存_shmmax与shmall计算
jvm·数据库·python