BM15 删除有序链表中重复的元素-I

/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */

/**
  * 
  * @param head ListNode类 
  * @return ListNode类
  */
function deleteDuplicates( head ) {
    // write code here
    if(head == null){
        return null
    }
    
    if(head.next == null) {
        return head
    }
    
    if(head.next.next == null && head.val == head.next.val){
        return head.next
    }


    
    let p = head
    while(p.next !== null) {
        if(p.val == p.next.val) {
            p.next = p.next.next
        }else {
            p = p.next
        }
         
    }
    
   return head
}
module.exports = {
    deleteDuplicates : deleteDuplicates
};

  

 

 

 

 

posted @ 2022-03-27 21:58  方头小小狮  阅读(34)  评论(0)    收藏  举报