Ctrl K

Thin Out a Sorted Chain

Easy
You are given a singly linked list head whose values are in non-decreasing order, so equal values sit next to each other. Keep exactly one node from every run of equal values, so that each value appears in the list only once. The remaining nodes keep their order, so the list stays sorted. Return the head of the resulting list.
Example 1
head=list_node([2, 2, 2, 5, 7, 7]) → [2, 5, 7]

The three 2s shrink to one, so do the two 7s; 5 appears once and stays as it is.

Example 2
head=list_node([-3, 0, 4]) → [-3, 0, 4]

There are no repeats, so the list does not change.

Example 3
head=list_node([]) → None

An empty list stays empty.

Constraints
  • The list has between 0 and 300 nodes
  • -100 ≤ Node.val ≤ 100
  • The values are in non-decreasing order
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZip Two Sorted Chains EasyCut Out Matching Nodes EasyFlip the Chain EasySymmetric Bead String EasyMidpoint of a List Easy
loading Python…
Input
head=list_node([2, 2, 2, 5, 7, 7])
Expected
[2, 5, 7]

Run checks the first 3 tests in your browser. Submit checks all 113 tests on the server, hidden ones included.