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
相关推荐
m0_6632340130 分钟前
Rust并发编程实践:10分钟入门系统级编程
python·算法·rust
奔跑吧邓邓子1 小时前
【Python爬虫(69)】解锁游戏数据宝藏:Python爬虫实战攻略
开发语言·爬虫·python·游戏
FreakStudio2 小时前
开源一款数据转换扩展板-FreakStudio多米诺系列
python·单片机·嵌入式·电子diy
久绊A2 小时前
Ubuntu及其衍生系统安装Python
开发语言·python
奔跑吧邓邓子3 小时前
【Python爬虫(64)】从“听”开始:Python音频爬虫与语音数据处理全解析
开发语言·爬虫·python·音频·语音识别
deephub4 小时前
用PyTorch从零构建 DeepSeek R1:模型架构和分步训练详解
人工智能·pytorch·python·深度学习·deepseek
xiao智4 小时前
Ansible 数百台批量操作前期准备工作
linux·python·ansible
浪子西科5 小时前
【数据结构】(Python)第六章:图
开发语言·数据结构·python
起个破名想半天了5 小时前
Web自动化之Selenium添加网站Cookies实现免登录
python·selenium·cookie
程序趣谈5 小时前
算法随笔_57 : 游戏中弱角色的数量
数据结构·python·算法