Count nodes of linked list | Practice | GeeksforGeeks

Given a singly linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.
Example 1:
Input:
LinkedList: 1->2->3->4->5
Output: 5
Explanation: Count of nodes in the 
linked list is 5, which is its length.

Example 2:
Input:
LinkedList: 2->4->6->7->5->1->0
Output: 7
Explanation: Count of nodes in the
linked list is 7. Hence, the output
is 7.
Your Task:
Your task is to complete the given function getCount(), which takes a head reference as an argument and should return the length of the linked list.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 105
1 <= value <= 103
 
0
prabhukumarsawraj4 hours ago
Using recursion to get result easy
class Solution
{
    public:
    //Function to count nodes of a linked list.
    int getCount(struct Node* head){
    
        //Code here
    if(head==NULL)
        return 0;
        else
        return getCount(head->next)+1;
    }
};
0
ghostlearner212 days ago
public static int getCount(Node head)   {       Node n = head;       int count = 0;       while(n!=null){           count++;           n = n.next;       }       return count;   }
0
svidushi16105 days ago
class Solution:   def getCount(self, head_node):       count= 0       curr= head_node              while curr != None:           count +=1           curr= curr.next       return count
0
mayank180919996 days ago
int getCount(struct Node* head)
    {
        int count=0;
        struct Node*temp=head;
        while(temp!=NULL){
            count++;
            temp=temp->next;
        }
      return count;
    }
+1
mohitraj31254 weeks ago
    int getCount(struct Node* head){       //Code here       int count=0;       while(head!=NULL)       {           count++;           head= head->next;       }       return count;   }
0
ankitmaurya02251 month ago
0
sachinmishraravi1 month ago
JAVA
class Solution
{
    //Function to count nodes of a linked list.
    public static int getCount(Node head)
    { Node current=head;
        int count=0;
        while(current!=null){
            count=count+1;
            current=current.next;
        }
        //Code here
        return count;
    }
}
 
0
ajinkyahimanshu0071 month ago
Simple and Easy to understand Python Code:
 
def getCount(self, head_node):       count = 0       curr = head_node              while curr:           count += 1           curr = curr.next       return count
0
atif836141 month ago
basic problem of link list :
int count=0;       if(head==null){           return 0;       }       while(head!=null){           count++;           head=head.next;       }       return count;   }
+1
arche1 month ago
C++ Very Simple Recursive Solution
int getCount(struct Node* head){  
   if(head==NULL) return 0;
   return 1 + getCount(head->next);    
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.