Linked List Length Even or Odd

[wpdm_package id=’1386′]

[wpdm_package id=’1390′]
Algorithm

  1. Move the pointer 2 nodes at a time. If it points to a null node means Linked list is of EVEN length, else it points to a node whose next pointer is NULL means Linked list is of ODD length.

Length_EvenOrOdd

[sourcecode lang=”cpp”]
int Check_EvenOrOdd(intnode *n){
while(n!=NULL && n->next!=NULL){
n=n->next->next;
}
if(!n)
return 1;
return 0;
}
[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *