icse-promo

Question

A linked list is formed from the objects of the class ,
class Node
{
int item;
Node next;
}
Write a method OR an algorithm to count the number of nodes in the linked list .The method declaration is specified below:
int count(Node ptr_start);

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					int count(Node ptr_start)
{		
    Node temp=new Node(ptr_start);
    int c=0;
    while(temp!=null)
    {	
        c++;
        temp=temp.next;
    }
    return c;
}

				
			

Coding Store

Leave a Reply

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