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
What is the output after deleting the first node?
Consider a doubly linked list with nodes 10 <-> 20 <-> 30. After deleting the first node, what is the printed list?
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_beginning(self): if not self.head: return if not self.head.next: self.head = None return self.head = self.head.next self.head.prev = None def print_list(self): current = self.head result = [] while current: result.append(current.data) current = current.next print(result) # Setup dll = DoublyLinkedList() dll.append(10) dll.append(20) dll.append(30) # Delete first node dll.delete_from_beginning() # Print list dll.print_list()
Attempts:
2 left
💡 Hint
Deleting from beginning removes the head node and updates the head pointer.
✗ Incorrect
Initially, the list is 10 <-> 20 <-> 30. Deleting the first node removes 10. The new head is 20, so the list becomes 20 <-> 30.
❓ Predict Output
intermediate2:00remaining
What happens when deleting from an empty doubly linked list?
Given an empty doubly linked list, what is the output after calling delete_from_beginning and then printing the list?
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 delete_from_beginning(self): if not self.head: return if not self.head.next: self.head = None return self.head = self.head.next self.head.prev = None def print_list(self): current = self.head result = [] while current: result.append(current.data) current = current.next print(result) # Setup empty_dll = DoublyLinkedList() # Delete first node empty_dll.delete_from_beginning() # Print list empty_dll.print_list()
Attempts:
2 left
💡 Hint
Deleting from an empty list should not change anything.
✗ Incorrect
The list is empty initially. Deleting from beginning does nothing. Printing the list shows an empty list [].
🔧 Debug
advanced2:00remaining
Identify the error in delete_from_beginning method
What error will this code raise when deleting from a list with 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 delete_from_beginning(self): if not self.head: return self.head = self.head.next self.head.prev = None # Setup single_node_list = DoublyLinkedList() single_node_list.head = Node(100) # Delete first node single_node_list.delete_from_beginning()
Attempts:
2 left
💡 Hint
Check what happens when head.next is None and you try to access head.prev.
✗ Incorrect
When the list has one node, head.next is None. After setting head = head.next, head becomes None. Then accessing head.prev causes AttributeError.
❓ Predict Output
advanced2:00remaining
What is the list after deleting from beginning twice?
Given a doubly linked list 5 <-> 15 <-> 25 <-> 35, what is the list after deleting from beginning two times?
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_beginning(self): if not self.head: return if not self.head.next: self.head = None return self.head = self.head.next self.head.prev = None def print_list(self): current = self.head result = [] while current: result.append(current.data) current = current.next print(result) # Setup dll = DoublyLinkedList() dll.append(5) dll.append(15) dll.append(25) dll.append(35) # Delete twice dll.delete_from_beginning() dll.delete_from_beginning() # Print list dll.print_list()
Attempts:
2 left
💡 Hint
Each deletion removes the head node and updates the head pointer.
✗ Incorrect
After first deletion, list is 15 <-> 25 <-> 35. After second deletion, list is 25 <-> 35.
🧠 Conceptual
expert1:00remaining
What is the time complexity of deleting from the beginning of a doubly linked list?
Choose the correct time complexity for deleting the first node in a doubly linked list of size n.
Attempts:
1 left
💡 Hint
Deleting from beginning only changes a few pointers.
✗ Incorrect
Deleting the first node requires updating the head pointer and possibly the prev pointer of the new head. This is done in constant time regardless of list size.