Remove Duplicates from Sorted List II

Medium#82dummy head

You get a sorted linked list. Delete every value that appears more than once, all of its copies, and keep only the values that appear exactly once. Return the head. Our example: 2 → 2 → 5 → 7 → 7 → 9 — the answer is 5 → 9.

input
head: the head of a linked list sorted in non-decreasing order, maybe empty.
returns
The head of the list with every value that repeats removed altogether: only the values met once stay, in their order.
constraints
  • 0 ≤ nodes ≤ 300
  • −100 ≤ Node.val ≤ 100
  • the list is sorted in non-decreasing order
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * 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}
 */
function deleteDuplicates(head) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more linked list pointers problems

Pattern “Linked list pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.