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 is string syntax?

553


Is passing by reference faster?

526


How does a for loop work java?

550


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

1909


What are the types of methods in java?

582






What are keywords in programming?

570


What does nextint () do in java?

554


What is a singleton puppy?

537


What is the program development process?

567


What is data string?

501


What is the difference between the final method and abstract method?

542


Can we override private constructor in java?

509


What is the final method?

606


How to use string tokenizer class.

624


What access modifiers can be used for methods?

563