Bird
Raised Fist0
DSA Javaprogramming~30 mins

Insert at End of Doubly Linked List in DSA Java - Build from Scratch

Choose your learning style10 modes available
Insert at End of Doubly Linked List
📖 Scenario: You are managing a playlist of songs. Each song is linked to the previous and next song, allowing you to move forward or backward easily. You want to add a new song at the end of the playlist.
🎯 Goal: Build a doubly linked list and write code to insert a new node at the end of the list.
📋 What You'll Learn
Create a doubly linked list with three nodes having data 10, 20, and 30
Create a variable called newData with value 40
Write code to insert a new node with newData at the end of the doubly linked list
Print the doubly linked list from head to end showing data values separated by ' <-> ' and ending with 'null'
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (back and forward buttons), and undo-redo features where moving both directions is needed.
💼 Career
Understanding linked lists is fundamental for software development roles, especially when working with low-level data structures or optimizing memory usage.
Progress0 / 4 steps
1
Create the initial doubly linked list
Create a doubly linked list with three nodes having data 10, 20, and 30. Use the class Node with int data, Node prev, and Node next. Create a variable head pointing to the first node.
DSA Java
Hint

Remember to link each node's next and prev pointers correctly.

2
Create the new data variable
Create an integer variable called newData and set it to 40.
DSA Java
Hint

Just create a simple integer variable named newData and assign 40.

3
Insert the new node at the end
Write code to insert a new node with data newData at the end of the doubly linked list starting from head. Traverse to the last node, then link the new node properly.
DSA Java
Hint

Traverse using a while loop until current.next is null. Then link the new node.

4
Print the doubly linked list
Print the doubly linked list starting from head. Print each node's data followed by ' <-> ', and end with 'null'. For example: 10 <-> 20 <-> 30 <-> 40 <-> null
DSA Java
Hint

Use a while loop to print each node's data followed by ' <-> '. End with 'null'.

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