2

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

asked Jul 27, 2018 at 10:18
2
  • If you know the count of the nodes, check the last node value and id is same as current node. Commented Jul 27, 2018 at 10:26
  • Probable duplicate of stackoverflow.com/questions/2663115/… Commented Jul 27, 2018 at 13:53

1 Answer 1

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,6497 gold badges20 silver badges31 bronze badges
answered Aug 2, 2018 at 18:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.