Challenge - 5 Problems
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reversing a doubly linked list
What is the printed state of the doubly linked list after reversing it using the given code?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None def reverse(head): current = head temp = None while current: temp = current.prev current.prev = current.next current.next = temp current = current.prev if temp: head = temp.prev return head # Create doubly linked list: 1 <-> 2 <-> 3 <-> 4 head = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) head.next = node2 node2.prev = head node2.next = node3 node3.prev = node2 node3.next = node4 node4.prev = node3 # Reverse the list head = reverse(head) # Print list forward result = [] current = head while current: result.append(current.data) current = current.next print(result)
Attempts:
2 left
💡 Hint
Think about how the prev and next pointers are swapped for each node.
✗ Incorrect
The reverse function swaps the prev and next pointers for each node, effectively reversing the list. After the loop, the head is updated to the last node processed, which becomes the new head. The printed list shows the nodes in reverse order.
🧠 Conceptual
intermediate1:30remaining
Understanding pointer changes in reversing a doubly linked list
When reversing a doubly linked list, which pointers are swapped for each node?
Attempts:
2 left
💡 Hint
Think about what makes the list reverse direction.
✗ Incorrect
Reversing a doubly linked list requires swapping both the next and prev pointers of each node to reverse the direction of traversal.
🔧 Debug
advanced2:00remaining
Identify the error in this reverse function for doubly linked list
What error will this code produce when reversing a doubly linked list?
DSA Python
def reverse(head): current = head while current: temp = current.next current.prev, current.next = current.next, current.prev current = temp return head
Attempts:
2 left
💡 Hint
Check what happens to the head pointer after swapping pointers.
✗ Incorrect
The code swaps pointers but does not update the head to the new start of the reversed list, so it returns the old head, which is now the tail.
❓ Predict Output
advanced1:30remaining
Output after reversing a doubly linked list with one node
What is the output after reversing a doubly linked list with a single node containing data 10?
DSA Python
class Node: def __init__(self, data): self.data = data self.prev = None self.next = None def reverse(head): current = head temp = None while current: temp = current.prev current.prev = current.next current.next = temp current = current.prev if temp: head = temp.prev return head head = Node(10) head = reverse(head) result = [] current = head while current: result.append(current.data) current = current.next print(result)
Attempts:
2 left
💡 Hint
Consider how reversing a single node list affects pointers.
✗ Incorrect
A single node reversed remains the same node with no next or prev nodes, so the output is [10].
🚀 Application
expert1:00remaining
Number of nodes after reversing a doubly linked list
After reversing a doubly linked list with 7 nodes, how many nodes does the reversed list contain?
Attempts:
1 left
💡 Hint
Reversing changes order but not the number of nodes.
✗ Incorrect
Reversing a doubly linked list only changes the direction of links, not the number of nodes.