Shu-How Zの小窝

Loading...

82. 删除排序链表中的重复元素 II

82. 删除排序链表中的重复元素 II

1:49:16

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
    if(!head) return null;
    let ret=new ListNode(-1,head),pre=ret,cur=head;
    while(cur&&cur.next){
        if(pre.next.val!=cur.next.val){
            cur=cur.next;
            pre=pre.next;
        }else{
            while(cur&&cur.next&&pre.next.val==cur.next.val){
                cur=cur.next;
            }
            pre.next=cur.next;
            cur=cur.next;
        }
    }
    return ret.next;
};

posted @ 2026-05-19 19:18  KooTeam  阅读(5)  评论(0)    收藏  举报