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");
}
}
Thursday, April 29, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment