Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Traversal in Doubly Linked List
Next article icon

Program to find size of Doubly Linked List

Last Updated : 04 Mar, 2025
Comments
Improve
Suggest changes
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<->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.

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)); 

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.

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)); 

Output
Size of the doubly linked list: 4 




Next Article
Traversal in Doubly Linked List

K

Kanishk_Verma
Improve
Article Tags :
  • DSA
  • Linked List
  • doubly linked list
Practice Tags :
  • Linked List

Similar Reads

  • Doubly Linked List in Python
    Doubly Linked List is a type of linked list in which each node contains a data element and two links pointing to the next and previous node in the sequence. This allows for more efficient operations such as traversals, insertions, and deletions because it can be done in both directions. Table of Con
    13 min read
  • Print Doubly Linked list in Reverse Order
    Given a doubly-linked list of positive integers. The task is to print the given doubly linked list data in reverse order. Examples: Input: List = 1 <=> 2 <=> 3 <=> 4 <=> 5 Output: 5 4 3 2 1 Input: 10 <=> 20 <=> 30 <=> 40 Output: 40 30 20 10 Approach: Take a
    7 min read
  • Merge Sort for Doubly Linked List
    Given a doubly linked list, The task is to sort the doubly linked list in non-decreasing order using merge sort.Examples: Input: 10 <-> 8 <-> 4 <-> 2Output: 2 <-> 4 <-> 8 <-> 10Input: 5 <-> 3 <-> 2Output: 2 <-> 3 <-> 5 Note: Merge sort for
    13 min read
  • 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
  • Find pairs with given sum in doubly linked list
    Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in a doubly-linked list whose sum is equal to the given value x in sorted order. Examples: Input: Output: (1, 6), (2,5)Explanation: We can see that there are two pairs (1, 6) and (2, 5) with sum 7. Input: Outp
    14 min read
  • Program to find average of all nodes in a Linked List
    Given a singly linked list. The task is to find the average of all nodes of the given singly linked list. Examples: Input: 7->6->8->4->1 Output: 26 Average of nodes: (7 + 6 + 8 + 4 + 1 ) / 5 = 5.2 Input: 1->7->3->9->11->5 Output: 6 Iterative Solution: Initialise a pointer
    7 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 Output: Reversed Doubly Linked List = 3 <-> 2 <-> 1 -> NULL Input: Doubly Linked List = 1 ->NULL Output: Reversed Doubly Linked List
    15 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences