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 a node by value in a singly linked list
What is the printed state of the linked list after deleting the node with value 3?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def append(self, val): if not self.head: self.head = Node(val) return current = self.head while current.next: current = current.next current.next = Node(val) def delete_by_value(self, val): current = self.head if current and current.val == val: self.head = current.next return prev = None while current and current.val != val: prev = current current = current.next if current: prev.next = current.next def print_list(self): current = self.head result = [] while current: result.append(str(current.val)) current = current.next print(" -> ".join(result) + " -> null") ll = LinkedList() for v in [1, 2, 3, 4, 5]: ll.append(v) ll.delete_by_value(3) ll.print_list()
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
✗ Incorrect
The node with value 3 is found and removed by linking the previous node (2) to the next node (4). The list after deletion is 1 -> 2 -> 4 -> 5 -> null.
❓ Predict Output
intermediate2:00remaining
Output after deleting the head node by value
What is the printed state of the linked list after deleting the node with value 1 (head node)?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def append(self, val): if not self.head: self.head = Node(val) return current = self.head while current.next: current = current.next current.next = Node(val) def delete_by_value(self, val): current = self.head if current and current.val == val: self.head = current.next return prev = None while current and current.val != val: prev = current current = current.next if current: prev.next = current.next def print_list(self): current = self.head result = [] while current: result.append(str(current.val)) current = current.next print(" -> ".join(result) + " -> null") ll = LinkedList() for v in [1, 2, 3, 4, 5]: ll.append(v) ll.delete_by_value(1) ll.print_list()
Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer moves to the next node.
✗ Incorrect
The head node with value 1 is deleted by moving the head pointer to the next node (2). The list after deletion is 2 -> 3 -> 4 -> 5 -> null.
🔧 Debug
advanced2:00remaining
Identify the error in delete_by_value method
What error will occur when deleting a node by value in the following code if the value is not found in the list?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def delete_by_value(self, val): current = self.head if current and current.val == val: self.head = current.next return prev = None while current and current.val != val: prev = current current = current.next prev.next = current.next ll = LinkedList() for v in [1, 2, 3]: ll.append(v) ll.delete_by_value(4)
Attempts:
2 left
💡 Hint
Consider what happens when the value is not found and current becomes None.
✗ Incorrect
If the value is not found, current becomes None. Then prev.next = current.next tries to access next of None, causing AttributeError.
❓ Predict Output
advanced2:00remaining
Output after deleting the last node by value
What is the printed state of the linked list after deleting the node with value 5 (last node)?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None class LinkedList: def __init__(self): self.head = None def append(self, val): if not self.head: self.head = Node(val) return current = self.head while current.next: current = current.next current.next = Node(val) def delete_by_value(self, val): current = self.head if current and current.val == val: self.head = current.next return prev = None while current and current.val != val: prev = current current = current.next if current: prev.next = current.next def print_list(self): current = self.head result = [] while current: result.append(str(current.val)) current = current.next print(" -> ".join(result) + " -> null") ll = LinkedList() for v in [1, 2, 3, 4, 5]: ll.append(v) ll.delete_by_value(5) ll.print_list()
Attempts:
2 left
💡 Hint
Deleting the last node means the previous node's next becomes None.
✗ Incorrect
The node with value 5 is removed by setting the next of node 4 to None. The list after deletion is 1 -> 2 -> 3 -> 4 -> null.
🧠 Conceptual
expert2:00remaining
Why is it important to handle deletion of the head node separately?
In the delete_by_value method, why do we check if the head node contains the value before traversing the list?
Attempts:
2 left
💡 Hint
Think about what changes when the first node is removed.
✗ Incorrect
The head pointer must be updated to the next node when the head node is deleted, so it requires special handling.