DSA Day 40/100

DSA Day 40/100

Topic: Linked List

Questions Successfully Completed: 1

1) Count nodes of linked list

Easy

Count nodes of linked list

Question
Input: LinkedList: 1->2->3->4->5 Output: 5 Explanation: Count of nodes in the linked list is 5, which is its length.
class Node{
    int data;
    Node next;
    Node(int a){  data = a; next = null; }
}
class Solution
{
    //Function to count nodes of a linked list.
    public static int getCount(Node head)
    {
        int count = 0;
        while(head!=null){
            count++;
            head = head.next;
        }       
        return count;       
    }
}

Thank you for reading :)