πŸ”— Doubly Linked List in Java – A Complete Beginner’s Guide (Part 1A)

Introduction, Need, Node Structure, Memory Representation, Head & Tail, and Creating Nodes

Welcome to the Doubly Linked List series!

In our previous series, we learned everything about the Singly Linked List, including insertion, deletion, traversal, searching, updating, counting, and reversing.

Although Singly Linked Lists are powerful, they have one major limitationβ€”they can only be traversed in one direction.

To overcome this limitation, computer scientists introduced the Doubly Linked List, a data structure where every node stores references to both the next node and the previous node.

In this tutorial, you’ll learn everything about the fundamentals of Doubly Linked Lists before we start performing insertion and deletion operations in the next part.


πŸ“š Table of Contents

  1. What is a Doubly Linked List?
  2. Why Do We Need a Doubly Linked List?
  3. Limitations of Singly Linked List
  4. Advantages of Doubly Linked List
  5. Structure of a Doubly Linked List
  6. Components of a Node
  7. Memory Representation
  8. Head and Tail
  9. Creating a Node in Java
  10. Java Program – Creating Nodes
  11. Time Complexity
  12. Frequently Asked Questions
  13. Conclusion

πŸ“– What is a Doubly Linked List?

A Doubly Linked List (DLL) is a linear data structure in which every node contains:

  • πŸ“¦ Data
  • ⬅️ Reference to the Previous Node
  • ➑️ Reference to the Next Node

Unlike a Singly Linked List, where movement is possible only in the forward direction, a Doubly Linked List allows traversal in both forward and backward directions.

Visual representation:

NULL ← [Prev | 10 | Next] ⇄ [Prev | 20 | Next] ⇄ [Prev | 30 | Next] β†’ NULL

Notice that every node knows both its previous and next neighbors.


πŸ€” Why Do We Need a Doubly Linked List?

Let’s consider a Singly Linked List.

10 β†’ 20 β†’ 30 β†’ 40 β†’ NULL

Suppose you are currently at node 30.

Now you want to move back to 20.

Can you?

❌ No.

Why?

Because node 30 only knows where 40 is.

It has no information about 20.

You would have to start from the head again.

This becomes inefficient for many applications.


❌ Limitations of Singly Linked List

Although Singly Linked Lists are useful, they have several limitations.

1️⃣ One-Way Traversal

10 β†’ 20 β†’ 30 β†’ 40

You can move

10 β†’ 20 β†’ 30 β†’ 40

But you cannot move

40 β†’ 30 β†’ 20

2️⃣ Deletion is More Difficult

Suppose we want to delete 30.

10 β†’ 20 β†’ 30 β†’ 40

We first need to find 20, because only the previous node can change its next reference.


3️⃣ Backward Navigation is Impossible

Applications like:

  • Browser Back Button
  • Undo Operations
  • Music Playlist Previous Song

cannot be efficiently implemented using a Singly Linked List.


βœ… Advantages of Doubly Linked List

A Doubly Linked List solves these problems.

βœ” Forward Traversal

βœ” Backward Traversal

βœ” Easier Deletion

βœ” Easier Insertion Before a Node

βœ” Efficient Navigation


🌍 Real-Life Analogy

Imagine you’re reading a book.

You can:

➑️ Turn to the next page.

⬅️ Go back to the previous page.

This is exactly how a Doubly Linked List works.

Another example is a browser.

Google

↓

YouTube

↓

Wikipedia

You can press:

⬅️ Back

➑️ Forward

This is one of the biggest applications of a Doubly Linked List.


πŸ”— Structure of a Doubly Linked List

Every node contains three parts.

+---------+--------+---------+
| Previous|  Data  |  Next   |
+---------+--------+---------+

Unlike a Singly Linked List, every node stores two references.


🧩 Components of a Node

⬅️ Previous

Stores the reference of the previous node.

Example

Previous

πŸ“¦ Data

Stores the actual information.

Example

25

➑️ Next

Stores the reference of the next node.


Complete node

+-----------+-------+-----------+
| Previous  | Data  |   Next    |
+-----------+-------+-----------+

🧠 Memory Representation

Suppose we have

10 ⇄ 20 ⇄ 30

Memory might look like this.

Address   Previous   Data   Next

100       NULL       10     250

250       100        20     400

400       250        30     NULL

Notice that

  • The first node has no previous node.
  • The last node has no next node.

Therefore,

NULL ← 10 ⇄ 20 ⇄ 30 β†’ NULL

πŸ“ Understanding Head and Tail

A Doubly Linked List generally uses two important references.

🟒 Head

Points to the first node.

Head

↓

10 ⇄ 20 ⇄ 30

πŸ”΄ Tail

Points to the last node.

10 ⇄ 20 ⇄ 30

             ↑

           Tail

Having a Tail makes several operations more efficient, such as:

  • Insertion at the end
  • Backward traversal

🎨 Visual Representation

Head                                         Tail
 ↓                                            ↑
NULL ← [10] ⇄ [20] ⇄ [30] ⇄ [40] β†’ NULL

πŸ€” Why Do We Need Tail?

Suppose we only have a Head.

To reach the last node,

we must traverse the entire list.

10

↓

20

↓

30

↓

40

Time Complexity

O(n)

Now suppose we also have a Tail.

Tail

↓

40

We can directly access the last node.

Time Complexity

O(1)

πŸ’» Creating a Node in Java

Every node is represented using a class.

class Node {

    int data;

    Node prev;

    Node next;

    Node(int data) {

        this.data = data;

        prev = null;

        next = null;

    }

}

πŸ“ Explanation

int data;

Stores the value.


Node prev;

Stores the previous node reference.


Node next;

Stores the next node reference.


Node(int data)

Constructor

Creates a new node.


prev = null;

next = null;

Initially,

the node has no neighbors.


πŸ’» Program 1 – Creating One Node

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

        System.out.println(head.data);

    }

}

Output

10

πŸ’» Program 2 – Creating Three Nodes

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

        first.next = second;

        second.prev = first;

        second.next = third;

        third.prev = second;

        System.out.println(first.data);

        System.out.println(second.data);

        System.out.println(third.data);

    }

}

Output

10

20

30

🎨 Visual Representation

NULL ← [10] ⇄ [20] ⇄ [30] β†’ NULL

More detailed view

NULL ← +------+ ←→ +------+ ←→ +------+ β†’ NULL
       | 10   |     | 20   |     | 30   |
       +------+     +------+     +------+

πŸ“ˆ Time Complexity

Creating one node

O(1)

Creating multiple nodes

O(n)

because one node is created at a time.


πŸ’Ύ Space Complexity

Each node stores

  • One integer
  • Previous reference
  • Next reference

Overall memory

O(n)

βš–οΈ Singly vs Doubly Linked List

FeatureSingly Linked ListDoubly Linked List
Previous Pointer❌ Noβœ… Yes
Next Pointerβœ… Yesβœ… Yes
Forward Traversalβœ… Yesβœ… Yes
Backward Traversal❌ Noβœ… Yes
Memory UsageLessMore
Insertion Before a NodeDifficultEasy
DeletionModerateEasier

❓ Frequently Asked Questions

Q1. What is a Doubly Linked List?

A Doubly Linked List is a linked list in which every node stores references to both the previous and next nodes.


Q2. Why is it called “Doubly”?

Because each node has two links:

  • Previous
  • Next

Q3. What is the advantage over a Singly Linked List?

The biggest advantage is bidirectional traversal. You can move both forward and backward through the list.


Q4. Why does a Doubly Linked List use more memory?

Each node stores an additional prev reference compared to a Singly Linked List.


Q5. Why do many implementations use a Tail pointer?

A Tail pointer provides direct access to the last node, making operations like insertion at the end and backward traversal more efficient.


🎯 Key Takeaways

  • πŸ”— A Doubly Linked List stores previous and next references.
  • ⬅️ It supports backward traversal.
  • ➑️ It supports forward traversal.
  • πŸ“¦ Each node contains three fields: prev, data, and next.
  • πŸ“ The Head points to the first node.
  • πŸ“ The Tail points to the last node.
  • πŸ’Ύ It uses more memory than a Singly Linked List but offers greater flexibility.
  • πŸš€ It is widely used in browser history, undo/redo systems, and navigation-based applications.

πŸŽ‰ Conclusion

A Doubly Linked List is an enhanced version of a Singly Linked List that overcomes the limitation of one-way traversal by introducing a previous reference in every node. This additional link makes operations like backward traversal, deletion, and insertion before a node much easier and more efficient.

In this part, you learned the fundamental concepts of Doubly Linked Lists, including why they are needed, how a node is structured, how memory is organized, the purpose of the Head and Tail pointers, and how to create nodes in Java.

πŸ‘‰ In Part 1B, we’ll continue by learning how to:

  • 🚢 Traverse a Doubly Linked List in the forward direction.
  • πŸ”™ Traverse it in the backward direction.
  • πŸ—οΈ Build a complete Doubly Linked List in Java.
  • πŸ’» Write complete Java programs with detailed dry runs and complexity analysis.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *