82. Remove Duplicates from Sorted List II

# medium

circle-check

Solution 1(complicated version):

very complicated
  1. head points current node, once head.val == head.next.val, it deletes all duplicates except itself.

  2. pre records pre-node of head and helps delete the first duplicates.

  3. if not encounter duplicates, both of pre and head move one step.

triangle-exclamation

Solution 2(easy version):

simple approach
  1. head represents new list.

  2. record duplicates value and delete all duplicates until head.next.val != head.next.next.val.

  3. otherwise head move one step.

circle-info

Don't move head until ensure the next element is not duplicated.

circle-exclamation

If deleting all duplicates required, we can record the value and delete them in on time; If deleting all duplicates except itself required, we can keep first one and delete others.

Last updated