1684838553 / arithmeticQuestions

程序员的算法趣题
2 stars 0 forks source link

链表的增删改查 #22

Open 1684838553 opened 1 year ago

1684838553 commented 1 year ago

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

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

示例 1: 输入:head = [1,1,2] 输出:[1,2]

示例 2: 输入:head = [1,1,2,3,3] 输出:[1,2,3]

来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

var deleteDuplicates = function(head) {
    if (!head) {
        return head;
    }

    let cur = head;
    while (cur.next) {
        if (cur.val === cur.next.val) {
            cur.next = cur.next.next;
        } else {
            cur = cur.next;
        }
    }
    return head;
};

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solution/shan-chu-pai-xu-lian-biao-zhong-de-zhong-49v5/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。