Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete Node at End in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at End Mastery
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 a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        temp.next = null;
    }

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

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(10);
        list.head.next = new Node(20);
        list.head.next.next = new Node(30);
        list.deleteAtEnd();
        list.printList();
    }
}
A10 -> 20 -> null
B10 -> 20 -> 30 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Think about how the last node is removed and what remains.
❓ Predict Output
intermediate
2:00remaining
Output after deleting last node from a single-node list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        temp.next = null;
    }

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

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(100);
        list.deleteAtEnd();
        list.printList();
    }
}
A100 -> null
BError: NullPointerException
Cnull
D100 -> 100 -> null
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in deleting last node from a linked list
What error will this code produce when deleting the last node from a list with two nodes?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtEnd() {
        if (head == null) return;
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp = null;
    }

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

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        list.head.next = new Node(2);
        list.deleteAtEnd();
        list.printList();
    }
}
A1 -> 2 -> null
B1 -> null
CNullPointerException
DCompilation error
Attempts:
2 left
💡 Hint
Check if the last node is actually removed from the list.
🧠 Conceptual
advanced
1:30remaining
Why is a previous pointer needed to delete the last node in singly linked list?
In a singly linked list, why do we need to keep track of the node before the last node to delete the last node?
ABecause the last node stores the address of the previous node
BBecause we need to set the previous node's next pointer to null to remove the last node
CBecause the head pointer always points to the last node
DBecause the last node cannot be accessed without a tail pointer
Attempts:
2 left
💡 Hint
Think about how links between nodes are updated when deleting.
🚀 Application
expert
2:30remaining
Resulting list after multiple deletions at end
Given the list 5 -> 10 -> 15 -> 20 -> null, what is the printed list after deleting the last node three times?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtEnd() {
        if (head == null) return;
        if (head.next == null) {
            head = null;
            return;
        }
        Node temp = head;
        while (temp.next.next != null) {
            temp = temp.next;
        }
        temp.next = null;
    }

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

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(5);
        list.head.next = new Node(10);
        list.head.next.next = new Node(15);
        list.head.next.next.next = new Node(20);
        list.deleteAtEnd();
        list.deleteAtEnd();
        list.deleteAtEnd();
        list.printList();
    }
}
Anull
B10 -> 15 -> 20 -> null
C5 -> 10 -> null
D5 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so track the list after each step.

Practice

(1/5)
1. What does the operation Delete Node at End do in a singly linked list?
easy
A. Removes the last node from the list
B. Removes the first node from the list
C. Adds a new node at the end of the list
D. Adds a new node at the beginning of the list

Solution

  1. Step 1: Understand the operation meaning

    Deleting a node at the end means removing the last node in the linked list.
  2. Step 2: Identify the correct description

    Removing the last node matches Removes the last node from the list, while others describe adding or removing nodes at different positions.
  3. Final Answer:

    Removes the last node from the list -> Option A
  4. Quick Check:

    Delete Node at End = Removes last node [OK]
Hint: Deleting at end always removes the last node [OK]
Common Mistakes:
  • Confusing delete at end with delete at beginning
  • Thinking it adds a node instead of removing
  • Mixing up first and last node operations
2. Which of the following is the correct Java syntax to delete the last node in a singly linked list named list assuming you have a method deleteAtEnd()?
easy
A. list.deleteAtEnd();
B. deleteAtEnd.list();
C. list.deleteAtEnd;
D. list.deleteAtEnd[];

Solution

  1. Step 1: Identify method call syntax in Java

    Calling a method on an object uses dot notation with parentheses: object.methodName();
  2. Step 2: Match correct syntax

    list.deleteAtEnd(); uses correct syntax: list.deleteAtEnd(); others have syntax errors or wrong usage.
  3. Final Answer:

    list.deleteAtEnd(); -> Option A
  4. Quick Check:

    Method call syntax = object.method() [OK]
Hint: Method calls need parentheses after name [OK]
Common Mistakes:
  • Omitting parentheses when calling methods
  • Using brackets [] instead of parentheses ()
  • Reversing object and method names
3. Consider the following Java code for a singly linked list:
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

class LinkedList {
  Node head;

  void deleteAtEnd() {
    if (head == null) return;
    if (head.next == null) {
      head = null;
      return;
    }
    Node temp = head;
    while (temp.next.next != null) {
      temp = temp.next;
    }
    temp.next = null;
  }

  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) {
    LinkedList list = new LinkedList();
    list.head = new Node(1);
    list.head.next = new Node(2);
    list.head.next.next = new Node(3);
    list.deleteAtEnd();
    list.printList();
  }
}
What is the output after running this code?
medium
A. 1 -> 2 -> 3 -> null
B. 1 -> 2 -> null
C. 2 -> 3 -> null
D. null

Solution

  1. Step 1: Understand initial list and deleteAtEnd()

    The list starts as 1 -> 2 -> 3 -> null. The deleteAtEnd() removes the last node (3).
  2. Step 2: Trace deleteAtEnd() method

    It traverses to node with data 2, sets its next to null, removing node 3.
  3. Final Answer:

    1 -> 2 -> null -> Option B
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting end removes last node, list shortens by one [OK]
Common Mistakes:
  • Forgetting to update the second last node's next to null
  • Assuming head changes when list has multiple nodes
  • Printing old list without deletion
4. The following Java method is intended to delete the last node of a singly linked list. What is the error?
void deleteAtEnd() {
  if (head == null) return;
  Node temp = head;
  while (temp.next != null) {
    temp = temp.next;
  }
  temp = null;
}
medium
A. It causes a NullPointerException
B. It does not handle empty list
C. It deletes the head node incorrectly
D. It does not update the second last node's next pointer

Solution

  1. Step 1: Analyze the loop and pointer updates

    The loop moves temp to the last node (where temp.next is null). Setting temp = null only changes local pointer, not list links.
  2. Step 2: Identify missing update to second last node

    The second last node's next pointer is not set to null, so the last node remains linked.
  3. Final Answer:

    It does not update the second last node's next pointer -> Option D
  4. Quick Check:

    Must update second last node's next to null [OK]
Hint: Set second last node's next to null, not just temp = null [OK]
Common Mistakes:
  • Setting temp = null only changes local variable
  • Not handling single-node list case
  • Ignoring list head update if only one node
5. You have a singly linked list with nodes containing values: 10 -> 20 -> 30 -> 40 -> null. You want to delete the last node and then add a new node with value 50 at the end. Which sequence of operations correctly achieves this?
hard
A. Call addAtEnd(50) only
B. Call addAtEnd(50), then call deleteAtEnd()
C. Call deleteAtEnd(), then call addAtEnd(50)
D. Call deleteAtEnd() twice, then addAtEnd(50)

Solution

  1. Step 1: Delete the last node

    Calling deleteAtEnd() removes node with value 40, list becomes 10 -> 20 -> 30 -> null.
  2. Step 2: Add new node with value 50 at end

    Calling addAtEnd(50) appends node 50, list becomes 10 -> 20 -> 30 -> 50 -> null.
  3. Final Answer:

    Call deleteAtEnd(), then call addAtEnd(50) -> Option C
  4. Quick Check:

    Delete last, then add new node [OK]
Hint: Delete old last node before adding new one [OK]
Common Mistakes:
  • Adding new node before deleting old last node
  • Deleting twice unnecessarily
  • Not deleting at all before adding
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