Javascript solution


  • 1
    R
    var removeElements = function(head, val) {
        
        var prev = null,
            actual = head;
        
        while(actual !== null){
            
            if(actual.val === val){
                if(prev === null){
                    head = actual.next;
                }else{
                    prev.next = actual.next;
                }
            }else{
                prev = actual;
            }
            
            actual = actual.next;
            
        }
        
        return head;
    };

  • 0
    T

    Hi, I don't understand if first element is not equal val, the head never changed.. why it can return right answer?


Log in to reply
 

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