Bird
Raised Fist0
DSA Javaprogramming~10 mins

Insert at End of Doubly Linked List in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End of Doubly Linked List
Create new node
↓
Is list empty?
↓
head = new
↓
Traverse to last node
↓
last.next = new
↓
new.prev = last
↓
Done
Create a new node, check if list is empty, if yes set head to new node; else traverse to last node and link new node at end.
Execution Sample
DSA Java
class Node {
  int data;
  Node prev, next;
  Node(int d) { data = d; prev = next = null; }
}

// Insert at end method
void insertEnd(int data) {
  Node newNode = new Node(data);
This code creates a new node and prepares to insert it at the end of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10EmptynewNode: data=10, prev=null, next=nullāˆ… (empty list)
2Check if list is emptyEmptyhead == null → Trueāˆ… (empty list)
3Set head = newNode1 nodehead → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Create new node with data=201 nodenewNode: data=20, prev=null, next=nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Check if list is empty1 nodehead == null → Falseā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Start at head to traverse1 nodecurrent → head (data=10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head/current
7Traverse to last node (current.next == null)1 nodecurrent.next == null → True (stop)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head/current
8Link newNode at end: current.next = newNode2 nodescurrent.next → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head newNode
9Set newNode.prev = current2 nodesnewNode.prev → currentā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head newNode
10Create new node with data=302 nodesnewNode: data=30, prev=null, next=nullā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current
11Check if list is empty2 nodeshead == null → Falseā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current
12Start at head to traverse2 nodescurrent → head (data=10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head/current
13Traverse: current.next != null? Yes (move current)2 nodescurrent → current.next (data=20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current
14Traverse: current.next != null? No (stop)2 nodescurrent.next == null → Trueā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │←── │ prev:──┼ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current
15Link newNode at end: current.next = newNode3 nodescurrent.next → newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:──┼←── │ prev:āˆ… │ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current newNode
16Set newNode.prev = current3 nodesnewNode.prev → currentā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:──┼←── │ prev:──┼ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head current newNode
17End of insertion3 nodesList updated with new node at endā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:──┼←── │ prev:──┼ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head node2 new tail
šŸ’” Traversal stops when current.next is null, indicating last node reached.
Variable Tracker
VariableStartAfter Step 3After Step 8After Step 9After Step 15After Step 16Final
headnullNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
newNodenullnullNode(20)Node(20)Node(30)Node(30)Node(30)
currentnullnullNode(10)Node(10)Node(20)Node(20)Node(20)
List Size0122333
Key Moments - 3 Insights
Why do we check if the list is empty before traversing?
Because if the list is empty (head == null), we directly set head to newNode (see step 2 and 3). Traversing would cause errors if list is empty.
Why do we stop traversal when current.next is null, not current itself?
Because current.next == null means current is the last node. We want to insert after last node, so we stop at last node (see step 7 and 14).
Why do we set newNode.prev = current after linking current.next = newNode?
To maintain backward links in doubly linked list. After linking forward pointer, we link backward pointer (see steps 8 and 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 9, what is the prev pointer of the new node?
APoints to the last node (data=10)
BPoints to null
CPoints to head
DPoints to itself
šŸ’” Hint
Check 'Pointer Changes' and 'Visual State' columns at step 9 in execution_table.
At which step does the list first have two nodes?
AStep 3
BStep 8
CStep 15
DStep 16
šŸ’” Hint
Look at 'Nodes in List' column in execution_table to find when it changes to 2 nodes.
If the list was empty, which step sets the head pointer to the new node?
AStep 1
BStep 2
CStep 3
DStep 4
šŸ’” Hint
Refer to 'Pointer Changes' at step 3 where head is assigned.
Concept Snapshot
Insert at End of Doubly Linked List:
- Create new node with data
- If list empty, head = new node
- Else traverse to last node
- Set last.next = new node
- Set new node.prev = last
- New node's next = null (tail)
Full Transcript
To insert at the end of a doubly linked list, first create a new node with the given data. Check if the list is empty by seeing if head is null. If empty, set head to the new node. If not empty, start at head and move through nodes until you find the last node (where next is null). Then set the last node's next pointer to the new node, and set the new node's prev pointer back to the last node. The new node's next pointer is null, marking it as the new tail. This process maintains both forward and backward links in the list.

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