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 sizeof () operator?

755


Does java linked list allow duplicates?

754


These static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { } }

817


What is toarray method in java?

782


What is %02d?

794


hr interview how many minutes asking question

1818


What is the difference between instanceof and isinstance?

775


hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea

1689


How do you add an element to an arraylist in java?

716


What are static variables and functions?

850


Which object oriented concept is achieved by using overloading and overriding?

823


What is a substitution variable?

810


Describe 2 different ways to concatenate two strings.

929


Do we need to manually write Copy Constructor?

798


How to make a write-only class in java?

809