【leetcode❤python】 203. Remove Linked List Elements

时间:2023-03-09 03:34:35
【leetcode❤python】 203. Remove Linked List Elements

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

class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        if head==None:return []
        dummy=ListNode(-1)
        dummy.next=head
        p=dummy
        
        while head:
            
            if head.val==val:
                p.next=head.next
                head=p
            
            p=head
            head=head.next
            
            
        return dummy.next