83/670

83. Remove Duplicates from Sorted List

Easy

You receive the head of a singly linked list whose values are sorted in non-decreasing order. Collapse every run of repeated values down to a single node, so that each distinct value appears exactly once, and return the head of the resulting list (still sorted).

For instance [1, 1, 2, 3, 3] becomes [1, 2, 3] — the first copy of each value is kept, the extra copies are unlinked.

Bypassing repeats: [1, 1, 2, 3, 3] → [1, 2, 3]
before11233cur.next = cur.next.next skips each repeatafter — one node per distinct value123

Example 1:

Input: head = [1, 1, 2]

Output: [1, 2]

Explanation: The second 1 is unlinked; one copy of each value remains.

Example 2:

Input: head = [1, 1, 2, 3, 3]

Output: [1, 2, 3]

Explanation: The repeated 1 and the repeated 3 are dropped, keeping one node per distinct value.

Constraints:

  • 0 ≤ number of nodes ≤ 300
  • -100 ≤ node value ≤ 100
  • The list is sorted in non-decreasing order.

Hints:

Sorted order means all copies of a value are consecutive, so you only ever need to compare a node with the one directly after it.

Unlike the harder version of this problem, the first node of every run always survives — so the head never changes and no dummy node is needed. When `cur.next` repeats `cur`'s value, bypass it with `cur.next = cur.next.next`; only advance `cur` when the next value differs.

▶ Run checks these sample cases. Submit also runs hidden edge cases.

Input: head = [1, 1, 2]

Expected output: [1, 2]