22. Remove duplicates from an unsorted linked list
The problem can be found at the following link: Question Link
My Approach
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 theskipItr
with the currentitr
.If it is not present in the hash map, mark the data as seen and proceed to the next node.
Time and Auxiliary Space Complexity
Time Complexity:
O(N)
, whereN
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.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated