Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

30:00

Remove Duplicates from Sorted List
easy
Free written solution
Topics
Companies

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:
Input: {"head":[1,1,2]}
Output: [1,2]
Constraints:
  • The number of nodes in the list is in the range [0,300][0, 300].

  • −100≤Node.val≤100-100 \leq \text{Node.val} \leq 100

  • The list is guaranteed to be sorted in ascending order.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"head":[1,1,2]}

The list is sorted, so every copy of a value sits right next to the others.

null112head

Unlinked duplicates (0)

none yet

List from head right now

[1, 1, 2]

Variables
VariableValue
currnull
curr.nextnull
removed0
DepthFunction Call
Stack empty
0/9