leetcode203 移除链表元素 --python

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        res = []
        while head:
            if head.val != val:
                res.append(head.val)
            head = head.next
        return res            

提交结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40924580/article/details/84204272