00:00

Doubly Linked List Data Structures

Last updated:

A doubly linked list is a linked list in which every node keeps two links: one to the node after it and one to the node before it. That second link lets you walk the list in both directions and remove a node without searching for its predecessor.

This page covers its structure, operations, a Java implementation, and when it is a better choice than a singly linked list.

What is Doubly Linked List in Data Structure?

A doubly linked list is a sequence of nodes in which each node has three fields: a prev reference to the previous node, the data, and a next reference to the following node. The first node's prev and the last node's next are null.

Because every node knows both neighbours, the list can be traversed forward from the head and backward from the tail.

Doubly linked list with prev and next links between neighbouring nodes

The three parts of a node:

Doubly linked list node: prev reference, data, next reference
  1. First Part: Pointer to point the previous node in the list.
  2. Second Part: The data (value) stored in the node.
  3. Third Part: Pointer to point the next node in the list.

Operations on Doubly Linked List

There are the following operations that can be performed on the doubly linked list.

  • Insertion:

    A new node can be added at the following positions of the doubly linked list.

    • Insert a node at the beginning
    • Insert a node at the end
    • Insert a node at the specific position
  • Deletion:

    An existing node can be deleted at the following positions of the doubly linked list.

    • Delete a node at the beginning
    • Delete a node at the end
    • Delete a node at the specific position
  • Traversal:

    Traversal can be performed in both directions, which is a major advantage of doubly linked lists.

    • Forward Traversal: Start traversing from the head and move through each node using the next pointers.
    • Backward Traversal: Start traversing from the tail and move through each node using the previous pointers.
  • Searching:

    We can search a specific node in the doubly linked list.

    • Search a specific node

Doubly Linked List Implementation in Java

DoublyLinkedList.java
// Java Program to implement Doubly Linked List
class Node {
  int data;         // Store node data
  Node next;        // Store address of the next node
  Node prev;        // Store address of the previous node

  public Node(int data) {
      this.data = data;
      this.next = null;
      this.prev = null;
  }
}

public class DoublyLinkedList {
    Node head;

    public 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;
    }

    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        // Create a doubly linked list
        DoublyLinkedList list = new DoublyLinkedList();

        // Insert nodes in the list
        list.insertNode(10);
        list.insertNode(20);
        list.insertNode(30);
        list.insertNode(40);
        list.insertNode(50);

        // Print the list
        list.printList();
    }
}
Output
10 20 30 40 50

When is a doubly linked list preferable to a singly linked list?

Neither list is better in every case; the choice depends on which operations the program performs most.

Choose a doubly linked list when you need to move backward as well as forward, when you often remove or insert next to a node you already hold (for example, a node found through a HashMap in an LRU cache), or when you need O(1) removal at both ends. If you only move forward and work mainly at the head, a singly linked list does the job with less memory.

Advantages of Doubly Linked List

  • Two-way traversal: the list can be walked from head to tail and from tail to head.
  • O(1) removal of a known node: the predecessor is available through prev, so no search from the head is needed.
  • O(1) insertion before or after a known node: only the neighbouring links change.
  • Fast operations at both ends: with head and tail references, insertion and deletion at either end are O(1), which makes it a natural deque.
  • No fixed capacity: like any linked list, it grows and shrinks one node at a time.

Disadvantages of Doubly Linked List

  • More memory per node: each node stores two references instead of one.
  • More links to keep correct: every insert and delete must update both next and prev; missing one leaves forward and backward traversal out of step.
  • Slightly more work per change: each operation updates more references than in a singly linked list, although the order of growth is the same.
  • Still no random access: reaching the k-th node takes O(k) steps (or O(n − k) from the tail).