83. Remove Duplicates from Sorted List(js)

83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
题意:对排好序的链表去重
代码如下:
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
         let node=head;
        
        while(node){
            if(!node.next){ 
                break;
            }
            //若相邻两项值相等,跳过,不相等继续下一项
            if(node.val==node.next.val){
                node.next=node.next.next;
            }else{
                node=node.next;
            }
            
        }
        return head;
};

 

posted @ 2019-04-07 20:35  mingL  阅读(80)  评论(0编辑  收藏  举报