Challenge 6: Detect Loop in a Linked List

Loops in linked lists can be dangerous. Now, you will create an algorithm to detect them.

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 DetectLoop() function, which will take a linked list as input, and deduce whether or not a loop is present.

Input

A doubly linked list is the input.

Output

It 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 80+ hands-on prep courses.