LeetCode - 83. Remove Duplicates from Sorted List | Lacerta

83. Remove Duplicates from Sorted List

BF

实现

二重循环遍历链表,去重即可

提示I - One Pass

实现

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
const deleteDuplicates = function(head) {
    let p = head;
    
    while (p !== null) {
      const c = p;
      
      while (p.next !== null && p.next.val === c.val) {
        p = p.next;
      }

      p = p.next;
      c.next = p;
    }
    
    return head;
};