Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting the last node from a singly linked list
What is the printed state of the linked list after deleting the last node?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_end(self): if not self.head: return if not self.head.next: self.head = None return second_last = self.head while second_last.next.next: second_last = second_last.next second_last.next = None def print_list(self): current = self.head while current: print(current.data, end=' -> ' if current.next else ' -> null\n') current = current.next ll = LinkedList() ll.append(10) ll.append(20) ll.append(30) ll.delete_end() ll.print_list()
Attempts:
2 left
💡 Hint
Deleting the last node removes the node with value 30.
✗ Incorrect
The list initially is 10 -> 20 -> 30 -> null. After deleting the last node (30), the list becomes 10 -> 20 -> null.
❓ Predict Output
intermediate2:00remaining
Output after deleting the last node from a single-node linked list
What is the printed state of the linked list after deleting the last node when the list has only one node?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_end(self): if not self.head: return if not self.head.next: self.head = None return second_last = self.head while second_last.next.next: second_last = second_last.next second_last.next = None def print_list(self): if not self.head: print('null') return current = self.head while current: print(current.data, end=' -> ' if current.next else ' -> null\n') current = current.next ll = LinkedList() ll.append(100) ll.delete_end() ll.print_list()
Attempts:
2 left
💡 Hint
Deleting the only node results in an empty list.
✗ Incorrect
The list initially has one node with value 100. After deleting the last node, the list becomes empty, so printing shows null only.
🔧 Debug
advanced2:00remaining
Identify the error in deleting the last node
What error does this code raise when trying to delete the last node from a linked list with two nodes?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_end(self): if not self.head: return current = self.head while current: current = current.next current.next = None ll = LinkedList() ll.append(1) ll.append(2) ll.delete_end()
Attempts:
2 left
💡 Hint
Check how the loop moves to the last node and what happens next.
✗ Incorrect
The code traverses past the last node to None, then tries to set current.next = None, causing AttributeError because current is None and has no 'next' attribute.
🧠 Conceptual
advanced1:30remaining
Why do we need to traverse to the second last node to delete the last node?
In a singly linked list, why must we stop at the second last node to delete the last node?
Attempts:
2 left
💡 Hint
Think about how nodes are connected in a singly linked list.
✗ Incorrect
In a singly linked list, each node points only to the next node. To remove the last node, we must set the second last node's next pointer to None, effectively cutting off the last node.
❓ Predict Output
expert2:30remaining
Output after multiple deletions at the end
What is the printed state of the linked list after deleting the last node three times from the initial list 5 -> 10 -> 15 -> 20 -> 25 -> null?
DSA Python
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def append(self, data): new_node = Node(data) if not self.head: self.head = new_node return last = self.head while last.next: last = last.next last.next = new_node def delete_end(self): if not self.head: return if not self.head.next: self.head = None return second_last = self.head while second_last.next.next: second_last = second_last.next second_last.next = None def print_list(self): current = self.head while current: print(current.data, end=' -> ' if current.next else ' -> null\n') current = current.next ll = LinkedList() for value in [5, 10, 15, 20, 25]: ll.append(value) ll.delete_end() ll.delete_end() ll.delete_end() ll.print_list()
Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after three deletions, three nodes are removed.
✗ Incorrect
Initial list: 5 -> 10 -> 15 -> 20 -> 25 -> null
After 1st deletion: 5 -> 10 -> 15 -> 20 -> null
After 2nd deletion: 5 -> 10 -> 15 -> null
After 3rd deletion: 5 -> 10 -> null