VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-size-doubly-linked-list/

⇱ Program to find size of Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find size of Doubly Linked List

Last Updated : 11 Jul, 2025

Given a doubly linked list, The task is to find the size of the given doubly linked list.

Example:

Input : 1<->2<->3<->4
output : 4
Explanation: The size is 4 as there are 4 nodes in the doubly linked list.

Input : 1<->2
output : 2

Approach - Using While Loop – O(n) Time and O(1) Space

The idea is to traverse the doubly linked list starting from the head node. Increment the size variable until we reach the end.


Output
4

Approach - Using Recursion - O(n) Time and O(n) Space

The idea is to use a recursive function that takes a node and check the next pointer of the current node if the next pointer is NULL then return 0 else if the next pointer is not NULL then return 1 + the value returned by the recursive function for the next node.


Output
Size of the doubly linked list: 4


Comment
Article Tags: