ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
How would you find a cycle in a linked list? 
 Question Submitted By :: Gvsk05
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How would you find a cycle in a linked list?
Answer
# 1
Simultaneously go through the list by ones (slow iterator)
and by twos (fast iterator). If there is a loop the fast
iterator will go around that loop twice as fast as the slow
iterator. The fast iterator will lap the slow iterator
within a single pass through the cycle. Detecting a loop is
then just detecting that the slow iterator has been lapped
by the fast iterator.

This solution is "Floyd's Cycle-Finding Algorithm" as
published in "Non-deterministic Algorithms" by Robert W.
Floyd in 1967. It is also called "The Tortoise and the Hare
Algorithm"
 
Is This Answer Correct ?    2 Yes 2 No
Sujith
 
  Re: How would you find a cycle in a linked list?
Answer
# 2
The above method is working of course, but is not the most
efficient. Other methods are however quite complex and not
so easy to explain.
Anyway, to exemplify this aforementioned method, maybe not
the most efficient code, but off the top of my head, hope
there are no misspellings.

bool isCyclic(LinkedNode *list)
{
if(list == NULL || list->next == NULL) return false;
LinkedNode *node1 = list, *node2 = node1->next;
while(node1 != node2)
{
  if(node1==NULL || node2==NULL || node2->next == NULL)
return false;
  node1 = node1->next;
  node2= node2->next->next;
}
return true;
}

NOTE: the assumption is that for noncyclic list, the last
node has next pointer set to NULL.
 
Is This Answer Correct ?    0 Yes 3 No
Jaroosh
 
 
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
What are data breakpoints? Adobe1
Consider a language that does not have arrays but does have stacks as a data type.and PUSH POP..are all defined .Show how a one dimensional array can be implemented by using two stacks. Google3
what is the defrenece between structure and union  5
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell6
Define function ?Explain about arguments? Geometric-Software2
how can i get output like this? 1 2 3 4 5 6 Excel3
C program to find frequency of each character in a text file?  3
Why cann't whole array can be passed to function as value.  1
How to add two numbers with using function?  3
fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is called as: fun(10); what will it print? } NDS13
Design a program using an array that searches a number if it is found on the list of the given input numbers and locate its exact location in the list.  3
What does a run-time "null pointer assignment" error mean?  2
how to find the binary of a number? Infosys5
wap in c to accept a number display the total count of digit  4
Main must be written as a.the first function in the program b.Second function in the program c.Last function in the program d.any where in the program TCS12
how can i make a program with this kind of output.. Enter a number: 5 0 01 012 0123 01234 012345 01234 0123 012 01 0 Wipro3
which one of follwoing will read a character from keyboard and store in c a)c=getc() b)c=getchar() c)c=getchar(stdin) d)getc(&c) e)none  5
What is structure packing ? HP1
differentiate between const char *a; char *const a; and char const *a; HCL1
why we shiuld use main keyword in C  5
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com