Question
A linked list is formed from the objects of the class ,
class Node
{
int num;
Node next;
}
Write a method OR an algorithm to print sum of nodes that contains only odd integers of an existing linked list .The method declaration is specified below:
void NodesCount(Node startptr);
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
void NodeCount(Node startPtr)
{
Nodes temp=new Nodes(startPtr);
int c=0;
while(temp!=null)
{
if(temp.num%2!=0)
{
c+=temp.num;
}
temp=temp.next;
}
System.out.println(c);
}