Python iterative solution with dummy node


  • 0
    W
    def removeElements(self, head, val):
        dummy = ListNode(-1)
        dummy.next = head
        pre, cur = dummy, head
        while cur:
            while cur and cur.val == val:
                cur = cur.next
            pre.next = cur
            pre = cur
            if not cur:
                return dummy.next
            cur = cur.next
        return dummy.next

Log in to reply
 

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