What is the most efficient way of finding a loop in linklist?

Traversing the link list with two pointers, both traversing at different speed.

Questions by Just for C

Showing Answers 1 - 5 of 5 Answers

deepak jain

  • Apr 24th, 2006
 

take 2 pointers, one incrementing once while other twice at a time.if both match at some time , then there is a loop otherwise not.

nimmi mathew

  • May 25th, 2006
 

Keep tracking the addresses of the nodes visited and each time checking if the address of the newly visited node is already there is the tracking list. If so there is a loop. Continue till this or if the end becomes null.

  Was this answer useful?  Yes

Shatrughan Mishra

  • Jun 22nd, 2006
 

We can have one member variable called "visited" of boolean type and initialized to false. whenever the node is being visited this variable is set to true. If on traversing, any node has this variable set to true initially, we conclude that Linked list has loop.

  Was this answer useful?  Yes

SIVA

  • Jul 16th, 2006
 

VERY NICE

  Was this answer useful?  Yes

madhuri

  • Aug 7th, 2006
 

just keep  a var in the node and assign it as yes on visiting and traverse .......then each time check this field in the node if not visited i.e, no then make it yes ............if already yes then u hav a loop

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions