Home » Data Structure & Algorithms » Detect Loop in LinkedList

Detect Loop in LinkedList


Question :

WAP to find if there is a loop exist in the linked list or not in  O(n) time complexity.

Algorithm :

Iterative Solution :

Step 1 : If head is null, then return -1.
Step 2 : Take two pointer fast and slow.Fast pointer moves by two position and slow pointer moves by one position.
Step 3 : When the fast pointer and slow pointer meets each other then there is a loop in the linked list else not.

OutPut :


Leave a comment

Your email address will not be published. Required fields are marked *

seventeen − eight =