Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete from End of Doubly Linked List in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void append(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 deleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node last = head;
        while (last.next != null) last = last.next;
        last.prev.next = null;
    }

    void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        }
        System.out.print("null\n");
    }

    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.append(10);
        dll.append(20);
        dll.append(30);
        dll.deleteFromEnd();
        dll.printList();
    }
}
A10 -> 20 -> null
Bnull
C20 -> 30 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Deleting from end removes the last node and updates the previous node's next pointer to null.
❓ Predict Output
intermediate
2:00remaining
Output after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last node when the list has only one node?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void append(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 deleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node last = head;
        while (last.next != null) last = last.next;
        last.prev.next = null;
    }

    void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        }
        System.out.print("null\n");
    }

    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.append(100);
        dll.deleteFromEnd();
        dll.printList();
    }
}
Anull
B100 -> null
CError
D100
Attempts:
2 left
💡 Hint
Deleting the only node should result in an empty list.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteFromEnd method
What error will occur when running this deleteFromEnd method on a doubly linked list with multiple nodes?
DSA Java
void deleteFromEnd() {
    if (head == null) return;
    Node last = head;
    while (last.next != null) last = last.next;
    last.next = null;
}
ANo error, works correctly
BThe last node is not deleted
CNullPointerException
DInfinite loop
Attempts:
2 left
💡 Hint
Consider what happens to the previous node's next pointer.
🧠 Conceptual
advanced
2:00remaining
Why update previous node's next pointer when deleting last node?
In a doubly linked list, why must we update the previous node's next pointer to null when deleting the last node?
ATo update the previous node's prev pointer
BTo make the list circular
CTo remove the reference to the deleted node and avoid memory leaks
DTo update the head pointer to the last node
Attempts:
2 left
💡 Hint
Think about what happens if the previous node still points to the deleted node.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions from end of doubly linked list
What is the printed state of the doubly linked list after deleting the last node three times in a row?
DSA Java
class Node {
    int data;
    Node prev, next;
    Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    Node head;

    void append(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 deleteFromEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node last = head;
        while (last.next != null) last = last.next;
        last.prev.next = null;
    }

    void printList() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        }
        System.out.print("null\n");
    }

    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.append(1);
        dll.append(2);
        dll.append(3);
        dll.append(4);
        dll.append(5);
        dll.deleteFromEnd();
        dll.deleteFromEnd();
        dll.deleteFromEnd();
        dll.printList();
    }
}
A1 -> 2 -> 3 -> null
B1 -> 2 -> 3 -> 4 -> null
Cnull
D1 -> 2 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions from a list of 5 nodes, 2 nodes remain.

Practice

(1/5)
1. What happens when you delete the last node from a doubly linked list that has multiple nodes?
easy
A. The second last node becomes the new tail and its next pointer is set to null.
B. The head pointer is set to null.
C. The list becomes circular.
D. The first node is deleted instead.

Solution

  1. Step 1: Identify the last node and its previous node

    In a doubly linked list, the last node's previous node is the second last node.
  2. Step 2: Update the second last node's next pointer

    Set the second last node's next pointer to null, making it the new tail.
  3. Final Answer:

    The second last node becomes the new tail and its next pointer is set to null. -> Option A
  4. Quick Check:

    Deleting last node updates tail's next to null [OK]
Hint: Tail's previous node next pointer becomes null after deletion [OK]
Common Mistakes:
  • Setting head to null instead of updating tail
  • Not updating the previous node's next pointer
  • Deleting the first node by mistake
2. Which of the following Java code snippets correctly deletes the last node from a doubly linked list named dll?
easy
A. dll.tail.next = dll.tail.prev; dll.tail = null;
B. dll.head = dll.head.next; dll.head.prev = null;
C. dll.tail = dll.tail.prev; dll.tail.next = null;
D. dll.head.prev = dll.tail; dll.tail = dll.tail.next;

Solution

  1. Step 1: Move tail pointer to previous node

    Set dll.tail to dll.tail.prev to remove last node reference.
  2. Step 2: Set new tail's next pointer to null

    Update dll.tail.next to null to mark end of list.
  3. Final Answer:

    dll.tail = dll.tail.prev; dll.tail.next = null; -> Option C
  4. Quick Check:

    Correct tail update and next pointer null [OK]
Hint: Tail moves back, next pointer set to null [OK]
Common Mistakes:
  • Updating head instead of tail
  • Setting tail to null without updating pointers
  • Confusing next and prev pointers
3. Consider the following Java code for deleting the last node from a doubly linked list:
class Node {
  int data;
  Node prev, next;
  Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
  Node head, tail;

  void deleteFromEnd() {
    if (tail == null) return;
    if (head == tail) {
      head = tail = null;
      return;
    }
    tail = tail.prev;
    tail.next = null;
  }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.head = new Node(1);
Node second = new Node(2);
dll.head.next = second;
second.prev = dll.head;
dll.tail = second;
dll.deleteFromEnd();
System.out.println(dll.head.data + " -> " + (dll.head.next == null ? "null" : dll.head.next.data));

What is the output?
medium
A. 1 -> 2
B. null -> null
C. 2 -> null
D. 1 -> null

Solution

  1. Step 1: Setup list with two nodes 1 and 2

    Head points to node 1, tail points to node 2, linked properly.
  2. Step 2: Delete last node (node 2)

    Tail moves to node 1, tail.next set to null, removing node 2.
  3. Final Answer:

    1 -> null -> Option D
  4. Quick Check:

    After deletion, only node 1 remains with next null [OK]
Hint: After deleting tail, tail.prev.next is null [OK]
Common Mistakes:
  • Printing old tail node data
  • Not updating tail.next to null
  • Confusing head and tail pointers
4. The following code attempts to delete the last node from a doubly linked list but causes a runtime error:
void deleteFromEnd() {
  if (tail == null) return;
  tail = tail.prev;
  tail.next = null;
}

What is the error and how to fix it?
medium
A. Compilation error due to missing semicolon; fix by adding semicolon.
B. NullPointerException when list has one node; fix by checking if tail.prev is null.
C. Logical error deleting head instead of tail; fix by updating head pointer.
D. No error; code works fine.

Solution

  1. Step 1: Identify cause of NullPointerException

    If list has one node, tail.prev is null, so tail becomes null, then tail.next causes error.
  2. Step 2: Fix by checking single node case

    Add condition: if head == tail, set head and tail to null before updating pointers.
  3. Final Answer:

    NullPointerException when list has one node; fix by checking if tail.prev is null. -> Option B
  4. Quick Check:

    Check single node case before pointer update [OK]
Hint: Check if tail.prev is null before accessing tail.next [OK]
Common Mistakes:
  • Not handling single node list
  • Assuming tail.prev always exists
  • Ignoring null pointer exceptions
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the last node twice using the correct deleteFromEnd method, what is the final list?
hard
A. 10 <-> 20
B. 10 <-> 20 <-> 30
C. 20 <-> 30
D. 30 <-> 40

Solution

  1. Step 1: Delete last node once

    Deleting last node 40 removes it; list becomes 10 <-> 20 <-> 30.
  2. Step 2: Delete last node second time

    Deleting last node 30 removes it; list becomes 10 <-> 20.
  3. Final Answer:

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

    Two deletions remove last two nodes [OK]
Hint: Each deleteFromEnd removes tail node [OK]
Common Mistakes:
  • Deleting from head instead of tail
  • Not updating tail pointer after deletion
  • Confusing node values after deletion
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