Home » Ask & Discuss » Board Exams - CBSE, ICSE, State Boards » Computer Science « Back to Discussion
Computer Science
Comments (4)

hello dear
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\");
}
}
take two pointers temp1 and temp2
initially
temp1 = head;
temp2 = head->next->next;
while(temp1!=Null && temp2!=Null)
{
if(temp1 == temp2)
return true; //list is circular
temp1=temp1->next;
temp2=temp2->next->next;
}
return false; //list is not circular
It is somewhat difficult to get it from here, but try using this algorithm on paper ..... take 8 nodes, make it circular by conecting last node with 3rd node.
Since list is circular temp2 will move to third node from 8th node. In this case temp1 == temp2 at node 5.
Complexity of this is also very less i.e. O(2n). ~ O(n)











