Traversal in Doubly Linked List
Last Updated :
19 Feb, 2025
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 <-> 40
Output: [10, 20, 30, 40]
and [40, 30, 20, 10]
Explanation:
- Forward traversal moves from the first node to the last:
[10, 20, 30, 40]
. - Backward traversal moves from the last node to the first:
[40, 30, 20, 10]
.
Input: 5 <-> 15 <-> 25 <-> 35 <-> 45 <-> 55
Output: [5, 15, 25, 35, 45, 55]
and [55, 45, 35, 25, 15, 5]
Explanation:
- Forward traversal follows the order of nodes:
[5, 15, 25, 35, 45, 55]
. - Backward traversal starts from the last node and moves backward:
[55, 45, 35, 25, 15, 5]
.
Input: 100 <-> 200 <-> 300
Output: [100, 200, 300]
and [300, 200, 100]
Explanation:
- Forward traversal reads nodes in order:
[100, 200, 300]
. - Backward traversal starts at the last node and moves to the first:
[300, 200, 100]
.
Types of Traversal in Doubly Linked List
Since each node of Doubly Linked List has pointer to the next node as well as the previous node, we can traverse the linked list in two directions:
- Forward Traversal
- Backward Traversal
Forward Traversal of Doubly Linked List
In Forward Traversal, we start from the first node, that is the head of the Doubly Linked List and continue visiting the next nodes using the next pointer of each node till we reach the last node of the linked list.
1. Iterative Approach for Forward Traversal
Follow the below steps:
- Initialize a pointer to the head of the linked list.
- While the pointer is not null:
- Visit the data at the current node.
- Move the pointer to the next node.
C++
// C++ Program for Forward Traversal (Iterative) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
Node(int val) {
data = val;
next = nullptr;
prev = nullptr;
}
};
// Function to traverse the doubly linked list
// in forward direction
void forwardTraversal(Node *head) {
// Start traversal from the head of the list
Node *curr = head;
// Continue until current node is not null
// (end of list)
while (curr != nullptr) {
// Output data of the current node
cout << curr->data << " ";
// Move to the next node
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node *head = new Node(1);
Node *second = new Node(2);
Node *third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Forward Traversal: ";
forwardTraversal(head);
return 0;
}
C
// C Program for Forward Traversal (Iterative) of
// Doubly Linked List
#include <stdio.h>
// Definition of a Node in a doubly linked list
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to traverse the doubly linked list
// in forward direction
void forwardTraversal(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Forward Traversal: ");
forwardTraversal(head);
return 0;
}
Java
// Java Program for Forward Traversal (Iterative) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GFG {
// Function to traverse the doubly linked list in
// forward direction
static void forwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
// Output data of the current node
System.out.print(curr.data + " ");
// Move to the next node
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Forward Traversal: ");
forwardTraversal(head);
}
}
Python
# Python Program for Forward Traversal (Iterative) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
# Function to traverse the doubly linked list
# in forward direction
def forward_traversal(head):
curr = head
while curr is not None:
# Output data of the current node
print(curr.data, end=" ")
# Move to the next node
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Forward Traversal: ", end="")
forward_traversal(head)
C#
// C# Program for Forward Traversal (Iterative) of
// Doubly Linked List
using System;
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int data) {
Data = data;
Next = null;
Prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list
// in forward direction
static void ForwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
// Output data of the current node
Console.Write(curr.Data + " ");
// Move to the next node
curr = curr.Next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Forward Traversal: ");
ForwardTraversal(head);
}
}
JavaScript
// Javascript Program for Forward Traversal (Iterative) of
// Doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Function to traverse the doubly linked list in forward direction
function forwardTraversal(head) {
let curr = head;
while (curr !== null) {
// Output data of the current node
console.log(curr.data + " ");
// Move to the next node
curr = curr.next;
}
console.log();
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
let head = new Node(1);
let second = new Node(2);
let third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Forward Traversal: ");
forwardTraversal(head);
OutputForward Traversal: 1 2 3
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(1)
2. Recursive Approach for Forward Traversal
Follow the below steps:
- Maintain a recursive function, say forwardTraversal(head) which takes the pointer to a node as parameter.
- Inside forwardTraversal(head)
- If the head pointer is NULL, then simply return from the function.
- Otherwise, print the data inside the node and call the recursive function with the next node, forwardTraversal(head->next).
C++
// C++ Program for Forward Traversal (Recursive) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
Node(int val) {
data = val;
prev = next = nullptr;
}
};
// Recursive function for forward traversal
void forwardTraversal(Node *head) {
if (head == nullptr)
return;
// Print current node's data
cout << head->data << " ";
// Recursive call with the next node
forwardTraversal(head->next);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node *head = new Node(1);
Node *second = new Node(2);
Node *third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Forward Traversal: ";
forwardTraversal(head);
return 0;
}
C
// C Program for Forward Traversal (Recursive) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Recursive function for forward traversal
void forwardTraversal(struct Node *head) {
if (head == NULL)
return;
// Print current node's data
printf("%d ", head->data);
// Recursive call with the next node
forwardTraversal(head->next);
}
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list: 1 <-> 2 <-> 3
struct Node *head = createNode(1);
struct Node *second = createNode(2);
struct Node *third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Forward Traversal: ");
forwardTraversal(head);
return 0;
}
Java
// Java Program for Forward Traversal (Recursive) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
// Constructor to initialize the node
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GfG {
// Recursive function for forward traversal
static void forwardTraversal(Node head) {
if (head == null) {
return;
}
// Print current node's data
System.out.print(head.data + " ");
// Recursive call with the next node
forwardTraversal(head.next);
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Forward Traversal: ");
forwardTraversal(head);
}
}
Python
# Python Program for Forward Traversal (Recursive) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
# Recursive function for forward traversal
def forward_traversal(head):
if head is None:
return
# Print current node's data
print(head.data, end=" ")
# Recursive call with the next node
forward_traversal(head.next)
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Forward Traversal:", end=" ")
forward_traversal(head)
C#
// C# Program for Forward Traversal (Recursive) of
// Doubly Linked List
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 {
// Recursive function for forward traversal
static void ForwardTraversal(Node head) {
if (head == null)
return;
// Print current node's data
Console.Write(head.Data + " ");
// Recursive call with the next node
ForwardTraversal(head.Next);
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Forward Traversal: ");
ForwardTraversal(head);
}
}
JavaScript
// JavaScript Program for Forward Traversal (Recursive) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
// Recursive function for forward traversal
function forwardTraversal(node) {
if (node === null) return;
// Print current node's data
console.log(node.data + " ");
// Recursive call with the next node
forwardTraversal(node.next);
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
let head = new Node(1);
let second = new Node(2);
let third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Forward Traversal: ");
forwardTraversal(head);
OutputForward Traversal: 1 2 3
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(n)
Backward Traversal of Doubly Linked List
In Backward Traversal, we start from the last node, that is the tail of the Doubly Linked List and continue visiting the previous nodes using the prev pointer of each node till we reach the first node of the linked list.
1. Iterative Approach for Backward Traversal
Follow the below steps:
- Initialize a pointer to the tail of the linked list.
- While the pointer is not null:
- Visit the data at the current node.
- Move the pointer to the previous node.
C++
// C++ Program for Backward Traversal (Iterative) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = prev = nullptr;
}
};
// Function to traverse the doubly linked list
// in backward direction
void backwardTraversal(Node* tail) {
// Start traversal from the tail of the list
Node* curr = tail;
// Continue until current node is not null
// (start of list)
while (curr != nullptr) {
// Output data of the current node
cout << curr->data << " ";
// Move to the previous node
curr = curr->prev;
}
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Backward Traversal: ";
backwardTraversal(third);
return 0;
}
C
// C Program for Backward Traversal (Iterative) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to traverse the doubly linked list
// in backward direction
void backwardTraversal(struct Node* tail) {
// Start traversal from the tail of the list
struct Node* curr = tail;
// Continue until current node is not null
while (curr != NULL) {
// Output data of the current node
printf("%d ", curr->data);
// Move to the previous node
curr = curr->prev;
}
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Backward Traversal: ");
backwardTraversal(third);
// Free the allocated memory
free(head);
free(second);
free(third);
return 0;
}
Java
// Java Program for Backward Traversal (Iterative) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list in
// backward direction
public static void backwardTraversal(Node tail) {
// Start traversal from the tail of the list
Node curr = tail;
// Continue until current node is not null
while (curr != null) {
// Output data of the current node
System.out.print(curr.data + " ");
// Move to the previous node
curr = curr.prev;
}
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Backward Traversal: ");
backwardTraversal(third);
}
}
Python
# Python Program for Backward Traversal (Iterative) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
def backward_traversal(tail):
curr = tail
# Traverse the list in backward direction
while curr is not None:
# Output data of the current node
print(curr.data, end=" ")
# Move to the previous node
curr = curr.prev
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Backward Traversal: ", end="")
backward_traversal(third)
C#
// C# Program for Backward Traversal (Iterative) of
// Doubly Linked List
using System;
public class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int val) {
Data = val;
Next = Prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list in backward direction
static void BackwardTraversal(Node tail) {
Node curr = tail;
// Continue until current node is not null (start of list)
while (curr != null) {
// Output data of the current node
Console.Write(curr.Data + " ");
// Move to the previous node
curr = curr.Prev;
}
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Backward Traversal: ");
BackwardTraversal(third);
}
}
JavaScript
// JavaScript Program for Backward Traversal (Iterative) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
// Function to traverse the doubly linked list in backward direction
function backwardTraversal(tail) {
let curr = tail;
// Continue until current node is not null (start of list)
while (curr !== null) {
// Output data of the current node
console.log(curr.data + " ");
// Move to the previous node
curr = curr.prev;
}
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Backward Traversal: ");
backwardTraversal(third);
OutputBackward Traversal: 3 2 1
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(1)
2. Recursive Approach for Backward Traversal
Follow the below steps:
- Maintain a recursive function, say backwardTraversal(node) which takes the pointer to a node as parameter.
- Inside backwardTraversal(node)
- If the head pointer is NULL, then simply return from the function.
- Otherwise, print the data inside the node and call the recursive function with the previous node, backwardTraversal(node->prev).
C++
// C++ Program for Backward Traversal (Recursive) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = prev = nullptr;
}
};
// Recursive function for backward traversal
void backwardTraversal(Node* node) {
if (node == nullptr) return;
// Print current node's data
cout << node->data << " ";
// Recursive call with the previous node
backwardTraversal(node->prev);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Backward Traversal: ";
backwardTraversal(third);
cout << endl;
return 0;
}
C
// C Program for Backward Traversal (Recursive) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Recursive function for backward traversal
void backwardTraversal(struct Node* node) {
if (node == NULL) return;
// Print current node's data
printf("%d ", node->data);
// Recursive call with the previous node
backwardTraversal(node->prev);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Backward Traversal: ");
backwardTraversal(third);
return 0;
}
Java
// Java Program for Backward Traversal (Recursive) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
public class GfG {
// Recursive function for backward traversal
static void backwardTraversal(Node node) {
if (node == null)
return;
// Print current node's data
System.out.print(node.data + " ");
// Recursive call with the previous node
backwardTraversal(node.prev);
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Backward Traversal: ");
backwardTraversal(third);
System.out.println();
}
}
Python
# Python Program for Backward Traversal (Recursive) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
def backward_traversal(node):
if node is None:
return
# Print current node's data
print(node.data, end=" ")
# Recursive call with the previous node
backward_traversal(node.prev)
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Backward Traversal: ", end="")
backward_traversal(third)
C#
// C# Program for Backward Traversal (Recursive) of
// Doubly Linked List
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 {
static void BackwardTraversal(Node node) {
if (node == null)
return;
// Print current node's data
Console.Write(node.Data + " ");
// Recursive call with the previous node
BackwardTraversal(node.Prev);
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Backward Traversal: ");
BackwardTraversal(third);
Console.WriteLine();
}
}
JavaScript
// Javascript Program for Backward Traversal (Recursive) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
function backwardTraversal(node) {
if (node === null) return;
// Print current node's data
console.log(node.data + " ");
// Recursive call with the previous node
backwardTraversal(node.prev);
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Backward Traversal: ");
backwardTraversal(third);
OutputBackward Traversal: 3 2 1
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(n)