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
相关推荐
文人sec7 分钟前
使用python-pandas-openpyxl编写运营查询小工具
开发语言·python·pandas
hu_yuchen19 分钟前
问卷系统自动化测试报告
软件测试·python
百锦再40 分钟前
第8章 模块系统
android·java·开发语言·python·ai·rust·go
Ashlee_code1 小时前
经纪柜台系统解析:从今日国际金融动荡看证券交易核心引擎的变革
python·架构·系统架构·区块链·vim·柜台·香港券商
清空mega1 小时前
从零开始搭建 flask 博客实验(4)
后端·python·flask
元亓亓亓2 小时前
LeetCode热题100--46. 全排列--中等
算法·leetcode·职场和发展
墨染点香2 小时前
LeetCode 刷题【146. LRU 缓存】
leetcode·缓存·哈希算法
qk学算法2 小时前
力扣滑动窗口题目-76最小覆盖子串&&1234替换子串得到平衡字符串
数据结构·算法·leetcode
小欣加油2 小时前
leetcode 860 柠檬水找零
c++·算法·leetcode·职场和发展·贪心算法
还是码字踏实2 小时前
基础数据结构之数组的矩阵遍历:螺旋矩阵(LeetCode 54 中等题)
数据结构·leetcode·矩阵·螺旋矩阵