My 1ms Java code which beats 87% of other java submissions


  • 0
    M
         if(head==null)
              return head;
    
        //Check to see if the head node's value is equal to the given value
        while(head!=null && head.val==val)
            head=head.next;
        
        ListNode curr=head;
        
      //Traverse through the list to remove the elements with the given value.
    
        while(head!=null && head.next!=null){
            if(head.next.val==val)
                head.next=head.next.next;
            else
                head=head.next;
        }
        
        return curr;

Log in to reply
 

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