Program to find size of Doubly Linked List
Last Updated :
04 Mar, 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.
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));
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));
OutputSize of the doubly linked list: 4