Remove All Duplicates from Sorted List

MediumLinked Listtwo pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the head of a linked list whose values are sorted in **non-decreasing** order. Delete every node whose value occurs **more than once** in the list, so only values that appeared exactly one time survive. This is stricter than ordinary de-duplication: if a value appears three times, all three nodes are removed, not two of them. Return the head of the resulting list, which remains sorted. If nothing survives, return an empty list.

Examples

Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
3 and 4 each appear twice, so every copy is removed.
Input: head = [1,1,1,2,3]
Output: [2,3]
All three 1s are removed.

Constraints

  • 0 <= number of nodes <= 300
  • -100 <= Node.val <= 100
  • The list is sorted in non-decreasing order

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More two pointers problems