Challenge - 5 Problems
Delete Node at End Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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(); } }
Attempts:
2 left
💡 Hint
Think about how the last node is removed and what remains.
✗ Incorrect
The method deleteAtEnd() removes the last node (30). The list after deletion is 10 -> 20 -> null.
❓ Predict Output
intermediateOutput 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(); } }
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
Since the list has only one node, deleting the last node sets head to null, so the list is empty and prints null.
🔧 Debug
advancedIdentify 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(); } }
Attempts:
2 left
💡 Hint
Check if the last node is actually removed from the list.
✗ Incorrect
The code sets the local variable temp to null but does not change the previous node's next pointer. So the list remains unchanged.
🧠 Conceptual
advancedWhy 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?
Attempts:
2 left
💡 Hint
Think about how links between nodes are updated when deleting.
✗ Incorrect
In singly linked lists, nodes only know their next node. To remove the last node, we must update the previous node's next to null, so we need to track it.
🚀 Application
expertResulting 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(); } }
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so track the list after each step.
✗ Incorrect
Deleting last node three times removes 20, then 15, then 10. The remaining list is 5 -> null.
