how to fing linkedlist is circular or not?

Answer Posted / pradip mishra(b-tech it)

Create two pointers, each set to the start of the list.
Update each as follows:

while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2)
pointer2=pointer2->next;
if (pointer1 == pointer2) {
print (\"circular\n\");
}
}

If a list is circular, at some point pointer2 will wrap
around and be either at the item just before pointer1, or
the item before that. Either way, it?s either 1 or 2 jumps
until they meet.

Is This Answer Correct ?    2 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what are the methods in object?

858


Is java map thread safe?

701


What is the return type of the main method?

788


What is the difference between C++ and Java and your preferences?

841


What is the default value of byte datatype in java?

683


What happens if an exception is throws from an object's constructor?

873


What is string syntax?

801


What is difference between == equals () and compareto () method?

731


What are the different types of methodologies?

756


What do you know about the garbage collector?

839


Is null in java?

743


What is the Concept of Encapsulation in OOPS

742


What is an empty class? What functionality does it offer in Java?

957


What is default size of arraylist in java?

772


Can a class have 2 constructors?

672