You are given the head of a linked list whose values are sorted from smallest to largest. Some values may appear on several nodes in a row. Remove nodes so that each value appears only once, and return the head of the list that is left. It stays sorted.
One of the two 2s goes, and two of the three 7s go. 5 and 9 already appear once.
An empty list has nothing to remove.
Constraints
0 and 300 nodes.-100 <= Node.val <= 100The list is sorted, so equal values always sit next to each other. A node has to go exactly when its value matches the node kept just before it.
The list is sorted, so every copy of a value sits in one unbroken run. All the 7s are side by side, never split up by a 9. That turns "does this value appear somewhere earlier?" into a much smaller question: does this node match the node right before it?
Time O(n)Space O(1)
The full walkthrough and code are part of Premium