Challenge - 5 Problems
Doubly 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 last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: 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 new_node.prev = last def delete_from_end(self): if not self.head: return if not self.head.next: self.head = None return last = self.head while last.next: last = last.next last.prev.next = None def print_list(self): if not self.head: print("null") return current = self.head result = [] while current: result.append(str(current.data)) current = current.next print(" -> ".join(result) + " -> null") # Setup dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) dll.delete_from_end() dll.print_list()
Attempts:
2 left
💡 Hint
Think about what happens to the last node and the previous node's next pointer.
✗ Incorrect
The last node (30) is removed. The previous node (20) now points to null, so the list ends at 20.
❓ Predict Output
intermediate2:00remaining
Output after deleting last node from single-node doubly linked list
What is the printed state of the doubly 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.prev = None self.next = None class DoublyLinkedList: 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 new_node.prev = last def delete_from_end(self): if not self.head: return if not self.head.next: self.head = None return last = self.head while last.next: last = last.next last.prev.next = None def print_list(self): if not self.head: print("null") return current = self.head result = [] while current: result.append(str(current.data)) current = current.next print(" -> ".join(result) + " -> null") # Setup single_node_list = DoublyLinkedList() single_node_list.append(100) single_node_list.delete_from_end() single_node_list.print_list()
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
When the only node is deleted, the head becomes None, so printing the list shows nothing, represented as 'null'.
🔧 Debug
advanced2:00remaining
Identify the error in delete_from_end method
Which option contains the error that will cause a broken list when deleting the last node from a doubly linked list with two nodes?
DSA Python
def delete_from_end(self): if not self.head: return if not self.head.next: self.head = None return last = self.head while last.next: last = last.next last.prev = None # Error here
Attempts:
2 left
💡 Hint
Think about which pointer needs to be updated to remove the last node properly.
✗ Incorrect
Setting last.prev = None breaks the link from the previous node to the last node but does not remove the last node from the list, causing a broken list.
🧠 Conceptual
advanced2:00remaining
Effect of deleting last node on doubly linked list pointers
After deleting the last node from a doubly linked list, which pointers are updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Consider what happens to the node before the last node after deletion.
✗ Incorrect
The previous node's next pointer must be set to None to indicate the new end of the list after the last node is removed.
❓ Predict Output
expert3:00remaining
Output after multiple deletions from end of doubly linked list
What is the printed state of the doubly linked list after deleting the last node three times in a row?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: 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 new_node.prev = last def delete_from_end(self): if not self.head: return if not self.head.next: self.head = None return last = self.head while last.next: last = last.next last.prev.next = None def print_list(self): if not self.head: print("null") return current = self.head result = [] while current: result.append(str(current.data)) current = current.next print(" -> ".join(result) + " -> null") # Setup dll = DoublyLinkedList() dll.append(1) dll.append(2) dll.append(3) dll.append(4) dll.append(5) dll.delete_from_end() dll.delete_from_end() dll.delete_from_end() dll.print_list()
Attempts:
2 left
💡 Hint
Each deletion removes the last node; track the list after each removal.
✗ Incorrect
Starting with 1->2->3->4->5, deleting last node thrice removes 5, then 4, then 3, leaving 1->2->null.