2

How to check whether linked list is circular or not without using extra memory if the head is given

1 Answers1

1

We can use two pointers here

  1. slow pointer which points to the head.

  2. fast pointer which points to the head as well.

Now slow pointer will traverse the list one by one(slow= slow.next)

while fast pointer will jump one node ahead (fast = fast.next.next).

So if they both meet in any position then there is a loop in the linkedlist or if the fast pointer terminates by pointing to the null value, It means there is no loop in the link list.

condition -> if(fast.value == slow.value) then there is a loop.

you can refer this link for more explanation -> https://www.geeksforgeeks.org/detect-loop-in-a-linked-list/

Nisarg
  • 1,631
  • 6
  • 19
  • 31