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
相关推荐
学Linux的语莫38 分钟前
python中类的常见写法
开发语言·python·状态模式
lvwangshu1 小时前
P17723 [SCP-S 2026] 函数 / func 题解
题解·双指针·技巧·性质题·思维好题·推导题
垆边人似月.1 小时前
Python中如何使用get函数实现频次查询
服务器·前端·python
AI 编程助手GPT2 小时前
GPT 脚本别只发一个文件:用 uv 把依赖和锁一起交付
人工智能·python·gpt·uv·依赖管理
Patrick在香港2 小时前
第 16,216 次连接之后,它不干了
python·网络编程·socket·tcp·连接池
我命由我123452 小时前
Python 开发 - Python 虚拟环境
运维·开发语言·后端·python·http·https·java-ee
云和数据.ChenGuang2 小时前
LLM大模型对接本地Ollama部署模型
开发语言·人工智能·python·langchain
文丑颜不良啊2 小时前
LeetCode276~300题目解析
java·leetcode
云和数据.ChenGuang3 小时前
光哥说Python 循环导入(Circular Import)深度解析:成因、排查与解决方案
开发语言·前端·人工智能·python·langchain
TomEval3 小时前
【测AI】第16篇:模型公平性与可解释性测试 — AI 的偏见与黑箱
人工智能·python·功能测试·ai·测试用例