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 the last node in a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public 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; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data + " -> "); temp = temp.next; } System.Console.WriteLine("null"); } } class Program { static void Main() { 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 what happens to the second last node's next pointer after deletion.
✗ Incorrect
The DeleteAtEnd method removes the last node (30). The second last node (20) now points to null, so the list ends at 20.
❓ Predict Output
intermediateOutput after deleting the last node in 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 C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public 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; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data + " -> "); temp = temp.next; } System.Console.WriteLine("null"); } } class Program { static void Main() { 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
When 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 the last node
What error will this code produce when deleting the last node?
DSA C#
public void DeleteAtEnd() {
if (head == null) return;
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp = null;
}Attempts:
2 left
💡 Hint
Setting temp to null does not change the linked list structure.
✗ Incorrect
The code only sets the local variable temp to null, which does not affect the previous node's next pointer. So the last node remains linked.
🧠 Conceptual
advancedTime complexity of deleting the last node in singly linked list
What is the time complexity of deleting the last node in a singly linked list of n nodes?
Attempts:
2 left
💡 Hint
You must traverse the list to find the second last node.
✗ Incorrect
To delete the last node, you need to find the node before it, which requires traversing the list once, so O(n).
❓ Predict Output
expertOutput after multiple deletions at end
What is the printed state of the linked list after deleting the last node three times in a row?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public 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; } public void PrintList() { Node temp = head; while (temp != null) { System.Console.Write(temp.data + " -> "); temp = temp.next; } System.Console.WriteLine("null"); } } class Program { static void Main() { LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.DeleteAtEnd(); list.DeleteAtEnd(); list.DeleteAtEnd(); list.PrintList(); } }
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so the list shrinks from the end.
✗ Incorrect
After three deletions, nodes 4, 3, and 2 are removed, leaving only node 1.
