Creating a Doubly Linked List, Forward Traversal & Backward Traversal in Java
Welcome to Part 1B of our Doubly Linked List in Java series.
In Part 1A, we learned:
- β What is a Doubly Linked List?
- β Why do we need a Doubly Linked List?
- β Limitations of Singly Linked List
- β Advantages of Doubly Linked List
- β Structure of a Node
- β Memory Representation
- β Head and Tail
- β Creating a Node in Java
Now it’s time to connect those nodes together and build a complete Doubly Linked List.
In this tutorial, you’ll learn:
- ποΈ Creating a Doubly Linked List
- πΆ Forward Traversal
- π Backward Traversal
- π» Complete Java Programs
- π Dry Runs
- π Time Complexity Analysis
- β Frequently Asked Questions
Let’s begin!
π Table of Contents
- Creating a Doubly Linked List
- Connecting Nodes
- Forward Traversal
- Backward Traversal
- Java Programs
- Dry Runs
- Time Complexity
- Advantages
- Frequently Asked Questions
- Conclusion
ποΈ Creating a Doubly Linked List
Creating a Doubly Linked List simply means connecting multiple nodes together using both next and previous references.
Suppose we want to create the following list.
10 β 20 β 30 β 40
Unlike a Singly Linked List,
every connection is made in both directions.
π¨ Visual Representation
Head Tail
β β
NULL β 10 β 20 β 30 β 40 β NULL
Notice that
- Each node points to the next node.
- Each node also points to the previous node.
π Connecting Nodes
Suppose we have
10
20
30
First,
Create all three nodes.
Node first = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Now connect them.
10 β 20 β 30
Connection 1
first.next = second;
second.prev = first;
Connection 2
second.next = third;
third.prev = second;
Notice something.
Every connection requires two assignments.
π§ Understanding the Connections
Suppose we have
10 β 20
Internally,
10.next = 20
20.prev = 10
Both references must be updated.
If either one is missing,
the list becomes incorrect.
π» Java Program β Creating a Doubly Linked List
class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList {
public static void main(String[] args) {
Node first = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Node fourth = new Node(40);
first.next = second;
second.prev = first;
second.next = third;
third.prev = second;
third.next = fourth;
fourth.prev = third;
System.out.println("Doubly Linked List Created Successfully.");
}
}
Output
Doubly Linked List Created Successfully.
πΆ Forward Traversal
Traversal means visiting every node exactly once.
Forward traversal starts from the Head and moves toward the Tail.
Example
Head
β
10 β 20 β 30 β 40 β NULL
Traversal Order
10
β
20
β
30
β
40
π Algorithm
- Start from Head.
- Print current node.
- Move to next node.
- Repeat until NULL.
π» Java Program β Forward Traversal
class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList {
public static void main(String[] args) {
Node head = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Node fourth = new Node(40);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
third.next = fourth;
fourth.prev = third;
Node temp = head;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}
Output
10 20 30 40
π Dry Run
Initially
Head
β
10 β 20 β 30 β 40
Iteration 1
temp = 10
Print 10
Move to 20
Iteration 2
temp = 20
Print 20
Move to 30
Iteration 3
temp = 30
Print 30
Move to 40
Iteration 4
temp = 40
Print 40
Move to NULL
Traversal completed.
π Backward Traversal
One of the biggest advantages of a Doubly Linked List is backward traversal.
Instead of starting from Head,
we start from the Tail.
Example
Head
β
10 β 20 β 30 β 40
β
Tail
Traversal Order
40
β
30
β
20
β
10
π€ Why Can’t We Do This in a Singly Linked List?
Because a Singly Linked List stores only
Next
A Doubly Linked List stores
Previous
Next
Therefore,
moving backward becomes possible.
π Algorithm
- Start from Tail.
- Print current node.
- Move to previous node.
- Repeat until NULL.
π» Java Program β Backward Traversal
class Node {
int data;
Node prev;
Node next;
Node(int data){
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList {
public static void main(String[] args){
Node first = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Node fourth = new Node(40);
first.next = second;
second.prev = first;
second.next = third;
third.prev = second;
third.next = fourth;
fourth.prev = third;
Node tail = fourth;
Node temp = tail;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.prev;
}
}
}
Output
40 30 20 10
π Dry Run
Initially
Tail
β
40
40
Move
30
30
Move
20
20
Move
10
10
Move
NULL
Stop.
π» Program β Forward and Backward Traversal Together
class Node {
int data;
Node prev;
Node next;
Node(int data){
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList {
public static void main(String args[]){
Node first = new Node(10);
Node second = new Node(20);
Node third = new Node(30);
Node fourth = new Node(40);
first.next = second;
second.prev = first;
second.next = third;
third.prev = second;
third.next = fourth;
fourth.prev = third;
Node head = first;
Node tail = fourth;
System.out.println("Forward Traversal:");
Node temp = head;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
System.out.println("Backward Traversal:");
temp = tail;
while(temp != null){
System.out.print(temp.data + " ");
temp = temp.prev;
}
}
}
Output
Forward Traversal:
10 20 30 40
Backward Traversal:
40 30 20 10
π Time Complexity
Creating Nodes
O(n)
Forward Traversal
Every node is visited once.
O(n)
Backward Traversal
Every node is visited once.
O(n)
πΎ Space Complexity
Only one temporary reference is used during traversal.
O(1)
π Advantages of Doubly Linked List Traversal
β Can move in both directions.
β Easy to implement browser history.
β Suitable for Undo/Redo operations.
β Efficient navigation.
β Frequently Asked Questions
Q1. Why does a Doubly Linked List require both prev and next?
The next reference allows forward traversal, while the prev reference allows backward traversal.
Q2. Can backward traversal be performed without a Tail pointer?
Yes, but you must first traverse from the Head to reach the last node. Maintaining a Tail pointer makes backward traversal start immediately.
Q3. Is forward traversal faster than backward traversal?
No. Both visit each node exactly once and have O(n) time complexity.
Q4. Why must we update both next and prev references while connecting nodes?
If only one reference is updated, the links become inconsistent, and the list cannot be traversed correctly in both directions.
Q5. Why does a Doubly Linked List consume more memory than a Singly Linked List?
Each node stores an additional prev reference, increasing the memory required per node.
π― Key Takeaways
- π Every connection in a Doubly Linked List requires updating both
nextandprev. - πΆ Forward traversal starts from the Head and follows
next. - π Backward traversal starts from the Tail and follows
prev. - π Maintaining both Head and Tail simplifies many operations.
- π Both forward and backward traversal have O(n) time complexity.
- πΎ Traversal requires only O(1) extra space.
π Conclusion
In this part, you learned how to build a Doubly Linked List, connect nodes correctly, and traverse the list in both forward and backward directions. These operations form the foundation for all advanced Doubly Linked List operations.
The ability to move in both directions is what makes a Doubly Linked List more flexible than a Singly Linked List, especially in applications such as browser navigation, media players, and undo/redo systems.
π In Part 2, we’ll begin performing Insertion Operations in a Doubly Linked List, including:
- β Insertion at the Beginning
- β Insertion at the End
- β Insertion at a Specific Position
- π§ Updating both
prevandnextreferences correctly - π» Complete Java programs
- π Detailed dry runs
- π Time complexity analysis