Question
A linked list is formed from the objects of the class Node. The class structure of the Node is given below:
class Node
{
int num;
Node next;
}
Write an Algorithm OR a Method to count the nodes that contain only odd integers from an existing linked list and returns the count.
The method declaration is as follows:
int CountOdd( Node startPtr )
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
int CountOdd(Node startPtr)
{
int c=0;
Node temp=startPtr;
while(temp != null)
{
if (temp.num % 2 != 0)
{
c++;
}
temp=temp.next;
}
return c;
}