溪语
Less Is More

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

示例 1:

输入: 1->1->2
输出: 1->2

示例 2:

输入: 1->1->2->3->3
输出: 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) {
    if(!head || !head.next){
        return head;
    }else {
        if(head.val === head.next.val){
            head.next = head.next.next
            deleteDuplicates(head)
        }else{
            deleteDuplicates(head.next)
        }
    }
    return head
    
};

 

posted on 2019-03-01 10:06  溪语_8023  阅读(80)  评论(0编辑  收藏  举报