399. Remove Duplicates from an Unsorted Linked List in O(n)

HardLinked ListHashLinked List

Given an unsorted linked list, remove all duplicate values so that each value appears only once, keeping the first occurrence of each value in its original order. Achieve O(n) time using a hash set. Return the resulting list as an array.

Input: An array of node values.

Output: Array — the de-duplicated list.

Examples

Example 1
Input: [1,2,1,3,2,4]
Output: [1,2,3,4]
Explanation: First occurrences kept.
Example 2
Input: [1,1,1,1]
Output: [1]
Explanation: All but one removed.
Example 3
Input: [1,2,3]
Output: [1,2,3]
Explanation: No duplicates.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →