Challenge: Detect Loop in a Linked List
Can you detect a loop in a linked list given as an input to your function? A solution is placed in the "solution" section for your help, but we would suggest you solve it on your own first.
We'll cover the following
Problem Statement #
In this problem, you have to implement the public static <T> boolean detectLoop(SinglyLinkedList<T> list)
method, which will take a Singly linked list as input and find if there is a loop present in the list. A loop in a linked list occurs if any node contains a reference to any previous node, then a loop will be formed. An illustration is also provided for your understanding.
Method Prototype #
public static <T> boolean detectLoop(SinglyLinkedList<T> list)
Output #
It returns true if a loop exists in the linked list; otherwise, false.
Sample Input #
linkedlist = 7->14->21->7
Sample Output #
true
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.