Easy to understand code AC


  • 0

    '''

    def removeElements(self, head, val):
        if head==None:
            return head #extreme conditions
        while head and head.val==val:
            head=head.next #remove all the preceding "val" objects
        su_head=head #keep a track of the new head of the list
    
        while head!= None and head.next!=None: '''remove all the other val-objects from the list'''
            if head.next.val==val:
                head.next=head.next.next
            else:
                head=head.next
        return su_head
    

    '''


Log in to reply
 

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