83 Remove Duplicates from Sorted List

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

重复的情况下指向下一个指针

  • 时间复杂度 O(1),空间复杂度O(1)
  • Runtime: 92 ms, faster than 64.01%
  • Memory Usage: 40.4 MB, less than 70.73%
/**
 * 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) {
    let res = head
    while(head && head.next) {
        while(head.next && head.val === head.next.val) {
            head.next = head.next.next
        }
        head = head.next
    }
    return res
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容