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

# 题目

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中   没有重复出现   的数字。

返回同样按升序排列的结果链表。

# 题解

# 遍历

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

  const dummy = new ListNode(0, head);

  let cur = dummy;
  while (cur.next && cur.next.next) {
    if (cur.next.val === cur.next.next.val) {
      const x = cur.next.val;
      while (cur.next && cur.next.val === x) {
        cur.next = cur.next.next;
      }
    } else {
      cur = cur.next;
    }
  }
  return dummy.next;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20