题解 | #删除有序链表中重复的元素-II#

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

https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024

/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @return ListNode类
 */
function deleteDuplicates( head ) {
    // write code here
    if (!head || !head.next) {
        return head;
    }
    const dummy = new ListNode(0);
    dummy.next = head;
    let prev = dummy;

    while (head !== null) {
        let hasDup = false;
        while (head.next !== null && head.val === head.next.val) {
            hasDup = true;
            head = head.next;
        }
        if (hasDup) {
            prev.next = head.next;
        } else {
            prev = prev.next;
        }
        head = head.next;
    }
    return dummy.next;
}
module.exports = {
    deleteDuplicates : deleteDuplicates
};

检查是否有重复元素,跳过重复元素

全部评论

相关推荐

05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务