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
相关推荐
XWalnut8 小时前
LeetCode刷题 day4
算法·leetcode·职场和发展
极梦网络无忧8 小时前
OpenClaw 基础使用说明(中文版)
python
codeJinger9 小时前
【Python】操作Excel文件
python·excel
旖-旎9 小时前
分治(库存管理|||)(4)
c++·算法·leetcode·排序算法·快速选择算法
XLYcmy9 小时前
一个针对医疗RAG系统的数据窃取攻击工具
python·网络安全·ai·llm·agent·rag·ai安全
Islucas10 小时前
Claude code入门保姆级教程
python·bash·claude
萝卜白菜。10 小时前
TongWeb7.0相同的类指明加载顺序
开发语言·python·pycharm
赵钰老师10 小时前
【ADCIRC】基于“python+”潮汐、风驱动循环、风暴潮等海洋水动力模拟实践技术应用
python·信息可视化·数据分析
爬山算法10 小时前
MongoDB(80)如何在MongoDB中使用多文档事务?
数据库·python·mongodb