In this article, we will learn, how to calculate the length of a linked list using the iterative and recursive method.

## Iterative method

An iterative method is the simplest way to calculate the length of the linked list. In the iterative method, we simply take a counter whose initial value is zero. Now we will trace the linked list till the last node using an iteration and increment the counter in each iteration.

## STEPS

• Initialize the counter with zero.
• Initialize a node pointer with the head pointer, pTmpNode = head.
• Trace the linked list until not getting the NULL pointer.
pTmpNode = pTmpNode -> pNextNode
• Increment counter on each iteration , iCounter ++.

OutPut:

## Recursive method

We can also find the length of the linked list using the recursive method. In which we will decrease the node and increase the counter in each recursive call.

Generally, people preferred the iterative method to calculate the length of the list because in the recursive method we are used stack memory in the calculation, if the size of the linked list is too long then might be you faced the stack overflow scenario.

## STEPS:

• If the head is NULL, return 0.
• Else return 1 + GetAndPrintTheList(pNode->pNextNode).

### Driver program to test the above function

OutPut:

Note: In recursion, you can also use the static variable to find the length of the variable.