Answer Posted / sujith
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 ? | 11 Yes | 2 No |
Post New Answer View All Answers
Why is this loop always executing once?
In a header file whether functions are declared or defined?
Is there any algorithm to search a string in link list in the minimum time?(please do not suggest the usual method of traversing the link list)
Why c is known as a mother language?
What is merge sort in c?
a c variable cannot start with a) an alphabet b) a number c) a special symbol d) both b and c above
#define PRINT(int) printf("int = %d ",int) main() {< BR> intx,y,z; x=03;y=02;z=01; PRINT(x^x); z<<=3;PRINT(x); y>>=3;PRINT(y); }
How does pointer work in c?
How can I check whether a file exists? I want to warn the user if a requested input file is missing.
What is a program?
Explain what is a static function?
How can I swap two values without using a temporary?
What is the difference between text files and binary files?
Differentiate between null and void pointers.
How can I do serial ("comm") port I/O?