Bird
Raised Fist0
DSA Javaprogramming

Insert at End of Doubly Linked List in DSA Java

Choose your learning style10 modes available
Mental Model
Add a new item at the very end by linking it after the last item and updating pointers.
Analogy: Like adding a new car to the end of a train, connecting it to the last car and making sure the last car points to it.
null ← 1 ↔ 2 ↔ 3 -> null
                 ↑ tail
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, insert value 4 at end
Goal: Add a new node with value 4 at the end of the doubly linked list
Step 1: Create new node with value 4
null ← 1 ↔ 2 ↔ 3 -> null   [new node: 4]
Why: We need a new node to insert at the end
Step 2: Link current tail node (3) next pointer to new node (4)
null ← 1 ↔ 2 ↔ 3 -> [4] -> null
                 ↑ tail
Why: Connect last node to new node to extend the list
Step 3: Link new node (4) previous pointer to current tail node (3)
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
                       ↑ tail
Why: Make new node point back to previous last node
Step 4: Update tail pointer to new node (4)
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
                           ↑ tail
Why: Tail must point to the new last node
Result:
null ← 1 ↔ 2 ↔ 3 ↔ 4 -> null
                           ↑ tail
Annotated Code
DSA Java
public class DoublyLinkedList {
    static class Node {
        int data;
        Node prev, next;
        Node(int data) {
            this.data = data;
            this.prev = null;
            this.next = null;
        }
    }

    Node head, tail;

    public DoublyLinkedList() {
        head = null;
        tail = null;
    }

    // Insert new node at the end
    public void insertAtEnd(int data) {
        Node newNode = new Node(data); // create new node
        if (head == null) { // empty list
            head = newNode;
            tail = newNode;
            return;
        }
        tail.next = newNode; // link current tail to new node
        newNode.prev = tail; // link new node back to tail
        tail = newNode; // update tail to new node
    }

    // Print list from head to tail
    public void printList() {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data);
            if (curr.next != null) System.out.print(" ↔ ");
            curr = curr.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.insertAtEnd(1);
        dll.insertAtEnd(2);
        dll.insertAtEnd(3);
        dll.insertAtEnd(4); // insert value 4 at end
        dll.printList();
    }
}
Node newNode = new Node(data); // create new node
create new node to insert
if (head == null) { head = newNode; tail = newNode; return; }
handle empty list by setting head and tail to new node
tail.next = newNode; // link current tail to new node
connect last node's next pointer to new node
newNode.prev = tail; // link new node back to tail
connect new node's prev pointer to old tail
tail = newNode; // update tail to new node
update tail pointer to new last node
OutputSuccess
1 ↔ 2 ↔ 3 ↔ 4
Complexity Analysis
Time: O(1) because we directly access the tail and update pointers without traversal
Space: O(1) because we only create one new node regardless of list size
vs Alternative: Compared to singly linked list without tail pointer, which requires O(n) traversal to insert at end, this method is faster with O(1) insertion
Edge Cases
empty list
new node becomes both head and tail
DSA Java
if (head == null) { head = newNode; tail = newNode; return; }
single element list
new node links after the single node and tail updates
DSA Java
tail.next = newNode; newNode.prev = tail; tail = newNode;
When to Use This Pattern
When you need to add an item at the end of a doubly linked list efficiently, look for the tail pointer to insert in O(1) time.
Common Mistakes
Mistake: Not updating the new node's prev pointer to the old tail
Fix: Set newNode.prev = tail to maintain backward link
Mistake: Not updating the tail pointer to the new node after insertion
Fix: Assign tail = newNode to keep tail accurate
Summary
Adds a new node at the end of a doubly linked list by updating next and prev pointers and tail.
Use when you want to append items quickly to the end of a doubly linked list.
The key is to link the new node after the tail and update tail to the new node.

Practice

(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To reverse the list
B. To remove the first node
C. To add a new element after the last node
D. To find the middle node

Solution

  1. Step 1: Understand the structure of a doubly linked list

    A doubly linked list has nodes connected both forward and backward, allowing traversal in both directions.
  2. Step 2: Purpose of insertion at the end

    Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
  3. Final Answer:

    To add a new element after the last node -> Option C
  4. Quick Check:

    Insert at end = Add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
  • Confusing insertion at end with deletion
  • Thinking insertion at end reverses the list
  • Assuming insertion at end finds middle node
2. Which of the following is the correct way to update the 'next' pointer of the last node when inserting a new node at the end in Java?
easy
A. last.next = newNode;
B. newNode.next = last;
C. last.prev = newNode;
D. newNode.prev = null;

Solution

  1. Step 1: Identify the last node's next pointer

    The last node's 'next' pointer should point to the new node being inserted.
  2. Step 2: Correct assignment for insertion

    Assigning 'last.next = newNode;' correctly links the last node to the new node.
  3. Final Answer:

    last.next = newNode; -> Option A
  4. Quick Check:

    Last node's next points to new node [OK]
Hint: Last node's next must point to new node [OK]
Common Mistakes:
  • Assigning newNode.next to last instead of last.next to newNode
  • Changing last.prev instead of last.next
  • Setting newNode.prev to null incorrectly
3. What will be the output of the following code snippet after inserting 40 at the end of the doubly linked list?
class Node {
  int data;
  Node next, prev;
  Node(int d) { data = d; next = prev = null; }
}

class DoublyLinkedList {
  Node head;
  void insertEnd(int data) {
    Node newNode = new Node(data);
    if (head == null) {
      head = newNode;
      return;
    }
    Node last = head;
    while (last.next != null) last = last.next;
    last.next = newNode;
    newNode.prev = last;
  }
  void printList() {
    Node temp = head;
    while (temp != null) {
      System.out.print(temp.data + " <-> ");
      temp = temp.next;
    }
    System.out.println("null");
  }
}

public class Main {
  public static void main(String[] args) {
    DoublyLinkedList dll = new DoublyLinkedList();
    dll.insertEnd(10);
    dll.insertEnd(20);
    dll.insertEnd(30);
    dll.insertEnd(40);
    dll.printList();
  }
}
medium
A. 10 <-> 20 <-> 30 <-> 40 <-> null
B. 40 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 20 <-> 30 <-> null
D. null

Solution

  1. Step 1: Trace insertions in order

    Nodes 10, 20, 30, then 40 are inserted at the end, so list grows as 10 <-> 20 <-> 30 <-> 40.
  2. Step 2: Print list traversal

    Print starts at head (10) and moves forward printing each node's data followed by ' <-> ', ending with 'null'.
  3. Final Answer:

    10 <-> 20 <-> 30 <-> 40 <-> null -> Option A
  4. Quick Check:

    Insertion at end appends nodes in order [OK]
Hint: Insertion order matches printed list [OK]
Common Mistakes:
  • Assuming new node is added at start
  • Forgetting to update newNode.prev
  • Printing list incorrectly or missing null
4. Identify the error in the following insertEnd method for a doubly linked list:
void insertEnd(int data) {
  Node newNode = new Node(data);
  if (head == null) {
    head = newNode;
  } else {
    Node last = head;
    while (last.next != null) {
      last = last.next;
    }
    last.next = newNode;
    newNode.next = last;
  }
}
medium
A. last.next should be set to head
B. The while loop condition should be last.prev != null
C. head should be updated to newNode when list is empty
D. newNode.next should be set to null, not last

Solution

  1. Step 1: Analyze pointer assignments after loop

    After finding last node, last.next is set to newNode correctly, but newNode.next is wrongly set to last (previous node).
  2. Step 2: Correct pointer for newNode.next

    Since newNode is last, its next should be null, and newNode.prev should point to last.
  3. Final Answer:

    newNode.next should be set to null, not last -> Option D
  4. Quick Check:

    Last node's next = newNode; newNode.next = null [OK]
Hint: Last node's next points newNode; newNode.next is null [OK]
Common Mistakes:
  • Setting newNode.next to last instead of null
  • Not updating newNode.prev to last
  • Incorrect loop condition in while
5. Given a doubly linked list with nodes 5 <-> 15 <-> 25, write the sequence of pointer updates needed to insert a new node with value 35 at the end. Which of the following correctly describes the steps?
hard
A. Set newNode.prev and newNode.next to null, head to newNode
B. Set newNode.prev to last node, last.next to newNode, newNode.next to null
C. Set newNode.next to last node, last.prev to newNode, newNode.prev to null
D. Set last.next to null, newNode.prev to null, newNode.next to last

Solution

  1. Step 1: Identify last node and new node pointers

    The last node is 25; newNode is 35. For insertion at end, newNode.prev must point to last node, and last.next must point to newNode.
  2. Step 2: Set newNode.next to null

    Since newNode is now last, its next pointer must be null to mark list end.
  3. Final Answer:

    Set newNode.prev to last node, last.next to newNode, newNode.next to null -> Option B
  4. Quick Check:

    Insert at end updates prev and next pointers correctly [OK]
Hint: New node prev = last; last next = new node; new node next = null [OK]
Common Mistakes:
  • Swapping prev and next pointers incorrectly
  • Not setting newNode.next to null
  • Changing head pointer unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard