Challenge: Detect Loop in a Linked List - Hashing
Loops in linked lists can be dangerous as they can end up as programs iterating linked lists indefinitely. Now, you'll create an algorithm to detect them.
We'll cover the following
Problem Statement
By definition, a loop is formed when a node in your linked list points to a previously traversed node.
You must implement the detect_loop()
function which will take a linked list as input and deduce whether or not a loop is present.
You have already seen this in Challenge: Detect Loop in a Linked List. Here you would use HashTables for a more efficient solution.
Input
A singly linked list.
Output
Returns True
if the given linked list contains a loop. Otherwise, it returns False
Sample Input
LinkedList = 7->14->21->7 # Both '7's are the same node. Not duplicates.
Sample Output
True
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.