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
相关推荐
XWalnut11 分钟前
LeetCode刷题 day37
java·数据结构·算法·leetcode
万山寒13 分钟前
python依赖包导出离线安装到没有外网的服务器
服务器·开发语言·python
程序员雷欧39 分钟前
LongAdder
开发语言·python
月光船幽幽1 小时前
分层阈值规避归藏协议过度重置
人工智能·python
SMF19191 小时前
【Linux】完美解决缩略图工具gm调用java.io.FileNotFoundException: gm问题
java·开发语言·python
㳺三才人子1 小时前
初探 Data Analysis - Matplotlib
python·plotly·pandas·matplotlib
Python私教1 小时前
0、null、未采集:AI Agent 反馈系统最容易踩的语义坑
人工智能·python
北斗落凡尘1 小时前
LangGraph 入门实战(5)
python·langchain
Python私教1 小时前
请求超时不等于失败:把写操作恢复设计成事实回读
后端·python
且听风吟02202 小时前
7.包管理工具
python