JAVA program for Insertion at the End of a Doubly Linked List

Insertion at the End of a Doubly Linked List in Java

Insertion at the End of a Doubly Linked List in Java is a fundamental data structure operation that allows you to add a new node after the current last node of the list. This operation is very important for building dynamic data structures such as music playlists, navigation systems, history management, and many real time applications where elements must be added at the end in a continuous manner.

Since a doubly linked list stores both previous and next references, insertion at the end becomes more efficient and organized compared to a singly linked list.

Insertion-at-the-End-of-a-Doubly-Linked-List-in-Java

Java Program for Insertion at the End of a Doubly Linked List

What Does Insertion at the End Mean?

A doubly linked list node contains three fields:

  • prev — points to the previous node
  • next — points to the next node

Insertion at the end means:

  1. Creating a new node.
  2. Placing it after the current last node (tail).
  3. Updating both next and prev pointers properly.

Example:

Before insertion:

10 ⇆ 20 ⇆ 30

Insert 40 at the end.

After insertion:

10 ⇆ 20 ⇆ 30 ⇆ 40

The new node becomes the last node of the list.

Understanding Insertion at the End of a Doubly Linked List using Java helps you learn:

  • Efficient node attachment.
  • Bi directional reference updates.
  • Difference between singly and doubly linked lists.
  • How tail based operations work?
Insertion at end in a doubly linked list

Methods for Insertion at the End of a Doubly Linked List in Java

Here to perform Insertion at the End of a Doubly Linked List using Java programming, we have mentioned 2 different methods:

  1. Method 1: Simple Traversal from Head
  2. Method 2: Using a Tail Pointer

Method for Insertion at the End of a Doubly Linked List in Java

Method 1: Simple Traversal from Head

Algorithm:

  1. Create a new node with given data.
  2. If head == null, return new node as head.
  3. Create a pointer temp = head.
  4. Traverse until temp.next == null.
  5. Set temp.next = newNode.
  6. Set newNode.prev = temp.
  7. Set newNode.next = null.
  8. Return the head.

Java Code:

Run
class InsertEndDoubly {

    static class Node {
        int data;
        Node prev;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public static Node insertAtEnd(Node head, int data) {
        Node newNode = new Node(data);

        if (head == null) {
            return newNode;
        }

        Node temp = head;

        while (temp.next != null) {
            temp = temp.next;
        }

        temp.next = newNode;
        newNode.prev = temp;
        newNode.next = null;

        return head;
    }

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

    public static void main(String[] args) {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.prev = head;
        head.next.next = new Node(30);
        head.next.next.prev = head.next;

        System.out.println("Before insertion:");
        printList(head);

        head = insertAtEnd(head, 40);

        System.out.println("After insertion:");
        printList(head);
    }
}

Input:

10 20 30
Insert: 40

Output:

10 20 30 40

Method for Insertion at the End of a Doubly Linked List in Java

Method 2: Using Tail Pointer

Algorithm:

This method keeps track of the last node, allowing insertion in constant time.

  1. Maintain a tail pointer in your class.
  2. Create a new node.
  3. If list is empty:
    • Set head = newNode

    • Set tail = newNode

  4. Else:

    • Set tail.next = newNode

    • Set newNode.prev = tail

    • Move tail = newNode

Java Code:

Run
class DoublyLinkedList {

    static class Node {
        int data;
        Node prev;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    Node head;
    Node tail;

    public void insertAtEnd(int data) {
        Node newNode = new Node(data);

        if (head == null) {
            head = tail = newNode;
            return;
        }

        tail.next = newNode;
        newNode.prev = tail;
        newNode.next = null;

        tail = newNode;
    }

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

    public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList();

        list.insertAtEnd(5);
        list.insertAtEnd(15);
        list.insertAtEnd(25);
        list.insertAtEnd(35);

        System.out.println("After insertion at end:");
        list.printList();
    }
}

Input:

Insert: 5, 15, 25, 35

Output:

5 15 25 35

Comparison between both methods:

MethodTime ComplexitySpace Complexity
Simple TraversalO(n)O(1)
Tail PointerO(1)O(1)

Insertion at the end of a doubly linked list highlights the advantage of having both forward and backward links in a data structure.

  • While simple traversal takes linear time, the use of a tail pointer enables constant time insertion, making the operation significantly more efficient for large datasets.
  • This operation strengthens understanding of bidirectional pointer management, memory organization, and dynamic list growth.

Learning Java Program for Insertion at the End of a Doubly Linked List prepares you for implementing complex data structures such as deques, operating system schedulers, and advanced memory handling systems.

FAQ's for Insertion at the End of a Doubly Linked List in Java

Answer:

It means adding a new node after the current last node by updating both next and previous pointers.

Answer:

Yes, especially when using a tail pointer, it runs in constant time O(1).

Answer:

It is optional, but it makes insertion at the end much faster.

Answer:

It is used in queues, browsers, music playlists, and dynamic data systems.

Answer:

Go to the last node and attach the new node by updating the next and prev references.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription