Write code to remove duplicates from an unsorted linked list.
Example1:

Example2:

Note:
The length of the list is within the range[0, 20000].
The values of the list elements are within the range [0, 20000].
Solution #1: Use extra memory to store element’s count
Since there is a fiexed value range, we can use a array to set flags for occured elements. Then, when we iterate the linked list, we test whether the current element occurred in array.
If not occured, insert it into final result. Otherwise, skip it.
Time complexity is O(N), space complexity is O(M), where M
is the max value of element.
Use a dummy
node to make code more simpler:

Solution #2: Scan twice
For the followup challenge, we can use more time to save memory. For each current node, we iterate the left LinkedList and skip all the nodes which with the same value of the current node.
Time complexity: O(N^2)

Join my Email List for more helpful insights, It's Free!😋