A museum's door log is a singly linked list of visitor card numbers in the order people came in, so equal numbers can be far apart. For the head count each visitor should appear only once: remove every node whose card number already appeared earlier in the log, keeping the first visit of each card where it was. Return the head.
Examples
Input: head = 3 -> 1 -> 3 -> 2 -> 1
Output: 3 -> 1 -> 2
Input: head = 1 -> 1 -> 1
Output: 1
Constraints
0 <= number of nodes <= 10**4- Target: O(n) time, O(n) extra space, one pass
Goals
- Use a set to remember values already kept
- Unlink repeated nodes while keeping the original order