Iterative JavaScript Solution


  • 0
    F
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @return {ListNode}
     */
    var deleteDuplicates = function(head) {
        if (head === null) return null;
        var current = head;
        while (current !== null) {
            while (current.next !== null && current.val === current.next.val) {
                current.next = current.next.next;
            }
            current = current.next;
        }
        return head;
    };

  • 0
    E

    when the input is [1,1]
    This solution falses


  • 0
    T
    var deleteDuplicates = function(head) {
        if(head == null){return null}
        var current = head;
        var bool = true;
        while(current.next != null){
            bool =true;
            if(current.val == current.next.val){
                if(current.next.next != null){
                    if(current.next.val == current.next.next.val){
                        bool = false;
                    }
                    current.next.val = current.next.next.val;
                    current.next = current.next.next;               
                }
                else{
                    current.next = null;
                    break;
                }
            }
            if(bool){
                current = current.next;
            }  
         }
        return head;
    };
    

Log in to reply
 

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