Given the head of a singly linked list, remove nodes so that each value appears exactly once and the node that survives is the last occurrence of that value. The surviving nodes keep their relative order. Return the head.
Examples
Input: head = 3 -> 1 -> 3 -> 2 -> 1
Output: 3 -> 2 -> 1
Explanation: the last 3 is at position 3, the last 2 at position 4 and the last 1 at position 5.
Input: head = 1 -> 2 -> 3
Output: 1 -> 2 -> 3
Constraints
0 <= number of nodes <= 10**4- Target: O(n) time, O(n) extra space
Goals
- Gather information in a first pass, delete in a second
- Use a counter to know whether a later copy of a value still exists