Doubly Linked List in Java Explained: A Beginner’s Guide

Table of Contents

When working with data structures in Java, choosing the right type of linked list can significantly impact performance and flexibility. While a Singly Linked List allows traversal in only one direction, a Doubly Linked List (DLL) offers bidirectional navigation. This makes it especially useful for applications where insertion, deletion, or reverse traversal operations are frequent.

In this guide, you’ll learn:

  • What a Doubly Linked List is
  • How it differs from a Singly Linked List
  • How to implement a Doubly Linked List in Java with code examples
  • Practical use cases, benefits, and common interview questions

What is a Doubly Linked List?

A Doubly Linked List is a dynamic data structure where each node contains three parts:

  1. Data — the value stored in the node
  2. Prev (previous reference) — pointer to the previous node
  3. Next (next reference) — pointer to the next node

This two-way linkage allows traversal both forward and backward, making it more versatile than a singly linked list.

Basic Node Structure in Java

Java
class Node {
    int data;
    Node prev;
    Node next;
}

Here:

  • data stores the value,
  • prev points to the previous node,
  • next points to the next node.

Key Characteristics of a Doubly Linked List

  • Bidirectional traversal — Move forward and backward.
  • Efficient deletion — A node can be deleted without explicitly having a pointer to its previous node (unlike singly linked lists).
  • More memory usage — Requires extra space for the prev pointer.
  • Dynamic size — Can grow or shrink as needed.

Visual Representation of a Doubly Linked List

Java
null <--- head ---> 1 <--> 10 <--> 15 <--> 65 <--- tail ---> null

Each node is connected in both directions, with:

  • head pointing to the first node
  • tail pointing to the last node

Implementation of a Doubly Linked List in Java

Defining the ListNode Class

Java
public class ListNode {
    int data;
    ListNode previous;
    ListNode next;

    public ListNode(int data) {
        this.data = data;
    }
}

Complete DoublyLinkedList Class

Java
public class DoublyLinkedList {
    private ListNode head;
    private ListNode tail;
    private int length;

    private class ListNode {
        private int data;
        private ListNode next;
        private ListNode previous;

        public ListNode(int data) {
            this.data = data;
        }
    }

    public DoublyLinkedList() {
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    public boolean isEmpty() {
        return length == 0;  // or head == null
    }

    public int length() {
        return length;
    }
}

This implementation provides:

  • A head pointer for the first node
  • A tail pointer for the last node
  • A length variable to track size
  • Utility methods isEmpty() and length()

You can extend this class further by adding insert, delete, and traversal methods.

Advantages of Doubly Linked List

  • Easy to reverse traverse the list
  • Deletion does not require a reference to the previous node
  • More flexible than singly linked lists

Disadvantages of Doubly Linked List

  • Requires extra memory for the prev pointer
  • Slightly more complex to implement compared to singly linked lists

Real-World Applications of Doubly Linked Lists

  • Navigating browser history (forward and backward navigation)
  • Undo/Redo functionality in editors
  • Deque (double-ended queue) implementations
  • Polynomial manipulation in compilers

FAQ: Doubly Linked List in Java

1. What is the difference between singly and doubly linked list?

A singly linked list allows traversal in only one direction, while a doubly linked list allows both forward and backward traversal.

2. Why use a doubly linked list instead of an array?

Unlike arrays, DLLs provide dynamic memory allocation and efficient insertion/deletion, especially in the middle of the list.

3. Does a doubly linked list use more memory?

Yes. Each node requires an extra pointer (prev), making it slightly heavier than a singly linked list.

4. What are common use cases of a doubly linked list?

They are used in text editors, music players, browsers, and deque implementations where bidirectional traversal is needed.

Conclusion

A Doubly Linked List in Java offers a balance between flexibility and efficiency. Its ability to traverse both forward and backward, along with efficient insertions and deletions, makes it ideal for applications requiring dynamic data handling.

If you’re preparing for Java coding interviews or working on real-world projects, mastering doubly linked lists will give you a strong foundation in data structures and algorithms.

Skill Up: Software & AI Updates!

Receive our latest insights and updates directly to your inbox

Related Posts

error: Content is protected !!