GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Concepts  >  Data Structures
Go To First  |  Previous Question  |  Next Question 
 Data Structures  |  Question 149 of 202    Print  
What is the most efficient way of finding a loop in linklist?
Traversing the link list with two pointers, both traversing at different speed.




  
Total Answers and Comments: 5 Last Update: August 07, 2006     Asked by: Just for C 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
April 24, 2006 00:06:39   #1  
deepak jain        

RE: What is the most efficient way of finding a loop i...
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.
 
Is this answer useful? Yes | No
May 25, 2006 01:34:59   #2  
nimmi mathew        

RE: What is the most efficient way of finding a loop i...
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.
 
Is this answer useful? Yes | No
June 22, 2006 05:14:09   #3  
Shatrughan Mishra        

RE: What is the most efficient way of finding a loop i...
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.
 
Is this answer useful? Yes | No
July 16, 2006 06:11:29   #4  
SIVA        

RE: What is the most efficient way of finding a loop i...
VERY NICE
 
Is this answer useful? Yes | No
August 07, 2006 12:33:31   #5  
madhuri        

RE: What is the most efficient way of finding a loop i...
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
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape