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
相关推荐
全栈凯哥21 分钟前
18.Python中的导入类完全指南
python
sunwenjian88644 分钟前
Java进阶——IO 流
java·开发语言·python
参.商.1 小时前
【Day41】143. 重排链表
leetcode·golang
guts3501 小时前
图像篡改数据集下载:COVERAGE、CASIA
python·数据集
森林猿1 小时前
java-modbus-读取-modbus4j
java·网络·python
2401_879693872 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
chushiyunen2 小时前
python chatTts实现tts文本转语音、音频
python
FreakStudio2 小时前
把 Flask 搬进 ESP32,高中生自研嵌入式 Web 框架 MicroFlask !
python·单片机·嵌入式·cortex-m3·异步编程·电子diy
love530love3 小时前
OpenClaw 手机直连配置全流程
人工智能·windows·python·智能手机·c#·agent·openclaw
chushiyunen3 小时前
python中的内置属性 todo
开发语言·javascript·python