Write an Algorithm OR a Method to count the number of nodes in the linked list The ,ethod declaration is given below:
int count (Node prt-start)
Algorithm to count the number of nodes in a linked list
Steps
- Start
- Set a temporary pointer to the first node and counter to 0
- Repeat steps 4 and 5 until the pointer reaches null
- Increment the counter
- Move the temporary pointer to the next node
- Return the counter value
- End
Method to count for the number of nodes in linked list
int count (node ptr_start)
{
Node a = new Node(ptr_start);
int c=0;
while (a!=null)
{
c++;
a= a.next;
}
return c;
}