Skip to content

How do you merge two sorted linked lists? #1677

Answered by Emojees
Kuefo asked this question in Q&A
Discussion options

You must be logged in to vote

[Naive Approach] Detect and Remove Loop using Hashing – O(n) Time and O(n) Space
The idea is to start traversing the Linked List from head node and while traversing insert each node into the HashSet. Also, maintain a prev pointer which points to the previous node of the current node. If there is a loop present in the Linked List, there will be a node which will be already present in the hash set.

If there is a node which is already present in the HashSet, then update the next pointer of prev to NULL to remove the loop from the linked list.
else, if there is no node which is already present in the HashSet , then there is no loop in the linked list.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Kuefo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants