22. Remove duplicates from an unsorted linked list
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To remove duplicates from a linked list, we can use a hash map to keep track of the elements we have encountered so far.
We traverse the linked list, and for each node.
Check if the data of the current node is already present in the hash map. If it is, skip the node by adjusting the pointers accordingly. To achieve this, I use skipItr
for iterating, effectively skipping all duplicate elements. Once all duplicate elements are skipped, I link the skipItr
with the current itr
.
If it is not present in the hash map, mark the data as seen and proceed to the next node.
Time Complexity: O(N)
, where N
represents the number of nodes in the linked list. Since we traverse the linked list once.
Auxiliary Space Complexity: O(N)
. In the worst-case scenario, where all elements in the linked list are unique, they will be stored in the unordered hash map.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.