Program to find size of Doubly Linked List Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a doubly linked list, The task is to find the size of the given doubly linked list. Example:Input : 1<->2<->3<->4output : 4Explanation: The size is 4 as there are 4 nodes in the doubly linked list. Input : 1<->2output : 2Approach - Using While Loop – O(n) Time and O(1) SpaceThe idea is to traverse the doubly linked list starting from the head node. Increment the size variable until we reach the end. C++ #include <bits/stdc++.h> using namespace std; class Node { public: int data; Node *next; Node *prev; Node(int val) { data = val; next = nullptr; prev = nullptr; } }; // This function returns size of linked list int findSize(Node *curr) { int size = 0; while (curr != NULL) { size++; curr = curr->next; } return size; } int main() { // Create a hard-coded doubly linked list: // 1 <-> 2 <-> 3 <-> 4 Node *head = new Node(1); head->next = new Node(2); head->next->prev = head; head->next->next = new Node(3); head->next->next->prev = head->next; head->next->next->next = new Node(4); head->next->next->next->prev = head->next->next; cout << findSize(head); return 0; } C #include <stdio.h> struct Node { int data; struct Node* prev; struct Node* next; }; int findSize(struct Node* curr) { int size = 0; while (curr != NULL) { size++; curr = curr->next; } return size; } struct Node *createNode(int new_data) { struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; new_node->prev = NULL; return new_node; } int main() { // Create a hard-coded doubly linked list: // 1 <-> 2 <-> 3 <-> 4 struct Node *head = createNode(1); head->next = createNode(2); head->next->prev = head; head->next->next = createNode(3); head->next->next->prev = head->next; head->next->next->next = createNode(4); head->next->next->next->prev = head->next->next; printf("%d\n", findSize(head)); return 0; } Java class Node { int data; Node next; Node prev; Node(int val) { data = val; next = null; prev = null; } } public class GfG { // This function returns the size of // the linked list static int findSize(Node curr) { int size = 0; while (curr != null) { size++; curr = curr.next; } return size; } public static void main(String[] args) { // Create a hard-coded doubly linked list: // 1 <-> 2 <-> 3 <-> 4 Node head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; System.out.println(findSize(head)); } } Python class Node: def __init__(self, val): self.data = val self.next = None self.prev = None # This function returns the size of # the linked list def findSize(curr): size = 0 while curr: size += 1 curr = curr.next return size if __name__ == "__main__": # Create a hard-coded doubly linked list: # 1 <-> 2 <-> 3 <-> 4 head = Node(1) head.next = Node(2) head.next.prev = head head.next.next = Node(3) head.next.next.prev = head.next head.next.next.next = Node(4) head.next.next.next.prev = head.next.next print(findSize(head)) C# using System; class Node { public int data; public Node next; public Node prev; public Node(int val) { data = val; next = null; prev = null; } } class GfG { // This function returns the size of // the linked list static int findSize(Node curr) { int size = 0; while (curr != null) { size++; curr = curr.next; } return size; } static void Main() { // Create a hard-coded doubly linked list: // 1 <-> 2 <-> 3 <-> 4 Node head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; Console.WriteLine(findSize(head)); } } JavaScript class Node { constructor(val) { this.data = val; this.next = null; this.prev = null; } } // This function returns the size // of the linked list function findSize(curr) { let size = 0; while (curr !== null) { size++; curr = curr.next; } return size; } // Create a hard-coded doubly linked list: // 1 <-> 2 <-> 3 <-> 4 let head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; console.log(findSize(head)); Output4Approach - Using Recursion - O(n) Time and O(n) SpaceThe 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. C++ #include <bits/stdc++.h> using namespace std; // Definition for doubly linked list node struct Node { int data; Node* next; Node* prev; Node(int val) : data(val), next(NULL), prev(NULL) {} }; // Function to calculate the size of the doubly linked list using recursion int findSize(Node* head) { if (head == NULL) return 0; return 1 + findSize(head->next); // Recursive call } int main() { // Creating a doubly linked list Node* head = new Node(1); head->next = new Node(2); head->next->prev = head; head->next->next = new Node(3); head->next->next->prev = head->next; head->next->next->next = new Node(3); head->next->next->next->prev = head->next->next; cout << findSize(head) << endl; return 0; } C #include <stdio.h> #include <stdlib.h> // Definition for doubly linked list node struct Node { int data; struct Node* next; struct Node* prev; }; // Function to calculate the size of the doubly linked list using recursion int findSize(struct Node* head) { if (head == NULL) return 0; return 1 + findSize(head->next); // Recursive call } // Function to create a new node struct Node* newNode(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; node->prev = NULL; return node; } int main() { // Creating a doubly linked list struct Node* head = newNode(1); head->next = newNode(2); head->next->prev = head; head->next->next = newNode(3); head->next->next->prev = head->next; head->next->next->next = newNode(4); head->next->next->next->prev = head->next->next; printf("%d\n", findSize(head)); return 0; } Java class GfG { // Definition for doubly linked list node static class Node { int data; Node next, prev; Node(int data) { this.data = data; next = prev = null; } } // Function to calculate the size of the doubly linked list using recursion public static int findSize(Node head) { if (head == null) { return 0; } return 1 + findSize(head.next); // Recursive call } public static void main(String[] args) { // Creating a doubly linked list Node head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; System.out.println(findSize(head)); } } Python class Node: def __init__(self, data): self.data = data self.next = None self.prev = None def findSize(head): if head is None: return 0 return 1 + findSize(head.next) # Recursive call # Creating a doubly linked list head = Node(1) head.next = Node(2) head.next.prev = head head.next.next = Node(3) head.next.next.prev = head.next head.next.next.next = Node(4) head.next.next.next.prev = head.next.next; print(findSize(head)) C# using System; class GfG { // Definition for doubly linked list node public class Node { public int data; public Node next; public Node prev; public Node(int data) { this.data = data; this.next = null; this.prev = null; } } // Function to calculate the size of the doubly linked list using recursion public static int findSize(Node head) { if (head == null) { return 0; } return 1 + findSize(head.next); // Recursive call } public static void Main(string[] args) { // Creating a doubly linked list Node head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; // Printing the size of the doubly linked list Console.WriteLine(findSize(head)); } } JavaScript class Node { constructor(data) { this.data = data; this.next = null; this.prev = null; } } function findSize(head) { if (head === null) { return 0; } return 1 + findSize(head.next); // Recursive call } // Creating a doubly linked list const head = new Node(1); head.next = new Node(2); head.next.prev = head; head.next.next = new Node(3); head.next.next.prev = head.next; head.next.next.next = new Node(4); head.next.next.next.prev = head.next.next; console.log(findSize(head)); OutputSize of the doubly linked list: 4 Comment More infoAdvertise with us Next Article Traversal in Doubly Linked List K Kanishk_Verma Follow Improve Article Tags : Misc Linked List DSA doubly linked list Practice Tags : Linked ListMisc Similar Reads Traversal in Doubly Linked List Traversal of Doubly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a doubly linked list and its implementation.Examples:Input: 10 <-> 20 <-> 30 <-> 40Output: 15+ min read Memory efficient doubly linked list We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the li 9 min read Operations of Doubly Linked List with Implementation A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in a singly linked list. Below are operations on the given DLL: Add a node at the front of DLL: The new node is always added before the head of the giv 15+ min read Find the largest node in Doubly linked list Given a doubly-linked list, find the largest node in the doubly linked list. Examples: Input: 10->8->4->23->67->88 Largest node is: 88 Output: 88 Input : 34->2->78->18->120->39->7 Largest node is: 120 Output :120 Approach Used: Initialize the temp and max pointer to 10 min read Reverse a Doubly Linked List Given a Doubly Linked List, the task is to reverse the Doubly Linked List.Examples:Input: Doubly Linked List = 1 <-> 2 <-> 3 -> NULL Doubly Linked List Output: Reversed Doubly Linked List = 3 <-> 2 <-> 1 -> NULLReversed Doubly Linked ListInput: Doubly Linked List = 1 - 15 min read Doubly Linked List Tutorial A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the prev 8 min read Like