-
I'm very curious on this and I would appreciate it if someone could answer my question on this subject, as I don't have the most experience, and I know there's programmers out there who could assist me on my question. thank you if you do decide to help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
[Naive Approach] Detect and Remove Loop using Hashing – O(n) Time and O(n) Space 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. |
Beta Was this translation helpful? Give feedback.
[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.