Thursday, April 29, 2010

Linked list

How do we check whether a linked list is circular?

Ans:
To check for it, create two pointers,and set each 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 linked list\n");
}
}

No comments: