Simple Iterative Python Solution


  • 0
    M
        def removeElements(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            d = ListNode(0)
            curr = d
            while head:
                if head.val != val:
                    curr.next = head
                    curr = curr.next
                head = head.next
    
            if curr.next and curr.next.val == val:
                curr.next = None
            
            return d.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.