Home » Data Structure & Algorithms » Length of Linked List

Length of Linked List

 


Question :

WAP to find the length of a linkedlist in O(n) time complexity.

Algorithm :

Iterative Solution :

Step 1: If head is null return 0

Step 2 : Traverse each node and increment the counter.The final value of counter is Length of LinkedList.

Recurssive Solution :

Step 1: If head is null return 0

Step 2 : Recurssively traverse each node  till the end and keep on incrementing the counter.The final value of counter is Length of LinkedList.

Program :

OutPut :

 
 


Leave a comment

Your email address will not be published. Required fields are marked *

4 × 1 =