Python 122 ns solution O(n) runtime O(1) space


  • 0
    P
        if head == None:
            return 
        
        curr = head
        while curr.next:
            # Found a node to delete 
            if curr.next.val == val:
                curr.next = curr.next.next
            else:
                curr = curr.next
    
        # Since we use curr.next, removing the head is a
        # special case, we do it at the end (here) in case
        # there are repeated key from head (avoid repeating 
        # removing the head)
        if head.val == val:
            return head.next
        return head

Log in to reply
 

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