To traverse a doubly linked list is to visit each of its nodes once, in order. Because every node has both a next and a prev reference, the walk can go in either direction: forward from the head or backward from the tail.
This tutorial shows both directions in Java, each written with a loop and with recursion.
Backward traversal is the main practical difference from a singly linked list. A singly linked list can only be printed in reverse with recursion or an explicit stack (O(n) extra memory); a doubly linked list just follows the prev links with O(1) extra memory.
Class: Node
Each Node object stores one value plus references to the previous and the next node. Both programs below use this class.
public class Node {
int data; // Data stored in the node
Node prev; // Reference to the previous node in the list
Node next; // Reference to the next node in the list
// Constructor to initialize a new node with data
Node(int new_data) {
data = new_data;
prev = null;
next = null;
}
}
1. Traverse a Doubly Linked List in Forward Direction (->)
Start at the head and keep following next until the reference becomes null. This is exactly how a singly linked list is traversed.
Class: ForwardTraversal
// Java Program to traverse a doubly linked list in forward (->) direction
// Using Iterative and Recursive approach
public class ForwardTraversal {
static Node head;
// Method to insert a node in doubly linked list
static void insertNode(int data) {
Node newNode = new Node(data);
Node last = head;
if (head == null) {
head = newNode;
return;
}
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
}
// Method to traverse using Iterative approach
static void forwardTraversalUsingIterative(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
// Method to traverse using Recursive approach
static void forwardTraversalUsingRecursive(Node head) {
if (head == null) {
return;
}
// Print current node's data
System.out.print(head.data + " ");
// Recursive call with the next node
forwardTraversalUsingRecursive(head.next);
}
public static void main(String[] args) {
// Create a doubly linked list
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
System.out.print("Forward Traversal using Iterative approach: ");
forwardTraversalUsingIterative(head);
System.out.print("Forward Traversal using Recursive approach: ");
forwardTraversalUsingRecursive(head);
}
}
Forward Traversal using Iterative approach: 10 20 30 40 50 Forward Traversal using Recursive approach: 10 20 30 40 50
Time Complexity: O(n) for both versions, where n is the number of nodes.
Extra Space: O(1) for the iterative version; O(n) for the recursive version, because each call adds a stack frame.
2. Traverse a Doubly Linked List in Backward Direction (<-)
Start at the last node (the tail) and keep following prev until the reference becomes null, which happens after the head has been printed. If only the head is known, walk forward to the tail first.
Class: BackwardTraversal
// Java Program to traverse a doubly linked list in backward (<-) direction
// Using Iterative and Recursive approach
public class BackwardTraversal {
static Node head;
// Method to insert a node in doubly linked list
static Node insertNode(int data) {
Node newNode = new Node(data);
Node last = head;
if (head == null) {
head = newNode;
return newNode;
}
while (last.next != null) {
last = last.next;
}
last.next = newNode;
newNode.prev = last;
return newNode;
}
// Method to traverse using Iterative approach
static void backwardTraversalUsingIterative(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;
}
}
// Method to traverse using Recursive approach
static void backwardTraversalUsingRecursive(Node node) {
if (node == null)
return;
// Print current node's data
System.out.print(node.data + " ");
// Recursive call with the previous node
backwardTraversalUsingRecursive(node.prev);
}
public static void main(String[] args) {
// Create a doubly linked list
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
Node last = insertNode(50);
System.out.print("Backward Traversal using Iterative approach: ");
backwardTraversalUsingIterative(last);
System.out.println();
System.out.print("Backward Traversal using Recursive approach: ");
backwardTraversalUsingRecursive(last);
}
}
Backward Traversal using Iterative approach: 50 40 30 20 10 Backward Traversal using Recursive approach: 50 40 30 20 10
Time Complexity: O(n) for both versions, where n is the number of nodes.
Extra Space: O(1) for the iterative version; O(n) for the recursive version, because each call adds a stack frame.