how to fing linkedlist is circular or not?



how to fing linkedlist is circular or not?..

Answer / 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

More Core Java Interview Questions

What is a method type?

1 Answers  


What is the difference between Checked and Unchecked exception? Give some examples

2 Answers   Ness Technologies,


What is collection class in java?

1 Answers  


How do you get length in java?

1 Answers  


What modifiers may be used with a top-level class?

1 Answers  


Can we have any code between try and finally blocks?

1 Answers  


what is the difference between abstract class and Interface?where we can use it in realtime projects?

23 Answers   Agile Software, Cognizant, IBM, Innodata, Logica CMG, Mitosis, Nagarro, Value Chain,


Differentiate between overriding and overloading cases?

1 Answers   Amdocs,


When do I need to use reflection feature in java?

1 Answers  


Is an array a vector?

1 Answers  


what is the use of reference variable

6 Answers  


What restrictions are placed on method overriding in java programming?

1 Answers  


Categories