Problem 119
Remove All Duplicates from Sorted List
Medium · 20 pts · Linked List, Two Pointers
Problem
You are given a linked list sorted in non-decreasing order. Delete every node whose value appears more than once, removing all copies of that value, not just the extras. Only the values that were unique in the original list remain. If nothing remains, print EMPTY. Use a dummy node before the head and a pointer to the last kept node. Whenever a run of equal values has length greater than 1, skip the whole run; otherwise keep its single node.
Input
First line: integer N, the number of nodes. Second line: the N node values from head to tail.
Output
Print the node values of the resulting list from head to tail, space-separated, or EMPTY if it has no nodes.
Constraints
0 <= N <= 300; -100 <= value <= 100; the list is sorted
Example input
7 1 2 3 3 4 4 5
Example output
1 2 5
Explanation
In 1 2 3 3 4 4 5, the values 3 and 4 appear twice, so all their nodes are removed. The values 1, 2 and 5 were unique, so the result is 1 2 5.