The idea is to traverse the linked list from the head node and count each node until the end of the list is reached.
Initialize count as 0 and curr as head.
Traverse the list while curr is not null.
Increment count and move curr to the next node.
Return count.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intnew_data){data=new_data;next=nullptr;}};intgetCount(Node*head){intcount=0;Node*curr=head;// Traverse till we reach nullptrwhile(curr!=nullptr){// Increment count by 1count++;// Move pointer to next nodecurr=curr->next;}returncount;}intmain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Node*head=newNode(1);head->next=newNode(3);head->next->next=newNode(1);head->next->next->next=newNode(2);head->next->next->next->next=newNode(1);cout<<getCount(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intgetCount(structNode*head){intcount=0;structNode*curr=head;// Traverse till we reach NULLwhile(curr!=NULL){// Increment count by 1count++;// Move pointer to next nodecurr=curr->next;}returncount;}intmain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1structNode*head=createNode(1);head->next=createNode(3);head->next->next=createNode(1);head->next->next->next=createNode(2);head->next->next->next->next=createNode(1);printf("%d",getCount(head));return0;}
Java
classNode{intdata;Nodenext;Node(intnewData){data=newData;next=null;}}publicclassGFG{publicstaticintgetCount(Nodehead){intcount=0;Nodecurr=head;// Traverse till we reach nullwhile(curr!=null){// Increment count by 1count++;// Move pointer to next nodecurr=curr.next;}returncount;}publicstaticvoidmain(String[]args){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);System.out.println(getCount(head));}}
Python
classNode:def__init__(self,new_data):self.data=new_dataself.next=NonedefgetCount(head):count=0curr=head# Traverse till we reach NonewhilecurrisnotNone:# Increment count by 1count+=1# Move pointer to next nodecurr=curr.nextreturncountif__name__=="__main__":# Create a hard-coded linked list:# 1 -> 3 -> 1 -> 2 -> 1head=Node(1)head.next=Node(3)head.next.next=Node(1)head.next.next.next=Node(2)head.next.next.next.next=Node(1)print(getCount(head))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intnewData){data=newData;next=null;}}classGFG{staticintgetCount(Nodehead){intcount=0;Nodecurr=head;// Traverse till we reach nullwhile(curr!=null){// Increment count by 1count++;// Move pointer to next nodecurr=curr.next;}// Return the count of nodesreturncount;}staticvoidMain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);Console.WriteLine(getCount(head));}}
JavaScript
classNode{constructor(newData){this.data=newData;this.next=null;}}functiongetCount(head){letcount=0;letcurr=head;// Traverse till we reach nullwhile(curr!==null){// Increment count by 1count++;// Move pointer to next nodecurr=curr.next;}returncount;}// Driver code// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1lethead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);console.log(getCount(head));
Output
5
Recursive Approach - O(n) Time and O(n) Space
The idea is to count the nodes recursively, starting from the head of the linked list.
If the current node is null, return 0.
Otherwise, return 1 + getCount(node.next), where 1 counts the current node.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*next;Node(intnew_data){data=new_data;next=nullptr;}};// Recursively count number of nodes in linked listintgetCount(Node*head){// Base Caseif(head==NULL){return0;}// Count this node plus the rest of the listreturn1+getCount(head->next);}intmain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Node*head=newNode(1);head->next=newNode(3);head->next->next=newNode(1);head->next->next->next=newNode(2);head->next->next->next->next=newNode(1);cout<<getCount(head);return0;}
C
#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}// Recursively count number of nodes in linked listintgetCount(structNode*head){// Base Caseif(head==NULL){return0;}// Count this node plus the rest of the listreturn1+getCount(head->next);}intmain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1structNode*head=createNode(1);head->next=createNode(3);head->next->next=createNode(1);head->next->next->next=createNode(2);head->next->next->next->next=createNode(1);printf("%d\n",getCount(head));return0;}
Java
classNode{intdata;Nodenext;Node(intnew_data){data=new_data;next=null;}}// Recursively count number of nodes in linked listpublicclassGFG{publicstaticintgetCount(Nodehead){// Base Caseif(head==null){return0;}// Count this node plus the rest of the listreturn1+getCount(head.next);}publicstaticvoidmain(String[]args){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);System.out.println(getCount(head));}}
Python
classNode:def__init__(self,new_data):self.data=new_dataself.next=None# Recursively count number of nodes in linked listdefgetCount(head):# Base CaseifheadisNone:return0# Count this node plus the rest of the listreturn1+getCount(head.next)if__name__=="__main__":# Create a hard-coded linked list:# 1 -> 3 -> 1 -> 2 -> 1head=Node(1)head.next=Node(3)head.next.next=Node(1)head.next.next.next=Node(2)head.next.next.next.next=Node(1)print(getCount(head))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intnewData){data=newData;next=null;}}classGFG{// Recursively count number of nodes in linked liststaticintgetCount(Nodehead){// Base Caseif(head==null){return0;}// Count this node plus the rest of the listreturn1+getCount(head.next);}staticvoidMain(){// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1Nodehead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);Console.WriteLine(getCount(head));}}
JavaScript
classNode{constructor(data){this.data=data;this.next=null;}}// Recursively count number of nodes in linked listfunctiongetCount(head){// Base Caseif(head===null){return0;}// Count this node plus the rest of the listreturn1+getCount(head.next);}// Driver code// Create a hard-coded linked list:// 1 -> 3 -> 1 -> 2 -> 1lethead=newNode(1);head.next=newNode(3);head.next.next=newNode(1);head.next.next.next=newNode(2);head.next.next.next.next=newNode(1);console.log(getCount(head));