Reverse a Doubly Linked List in DSA Python - Time & Space Complexity
We want to understand how the time needed to reverse a doubly linked list changes as the list grows.
Specifically, how does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def reverse_doubly_linked_list(head):
current = head
while current:
current.prev, current.next = current.next, current.prev
head = current
current = current.prev
return head
This code reverses the links of a doubly linked list so the last node becomes the first.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so n times for n nodes.
As the list grows, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list gets longer.
Time Complexity: O(n)
This means the time to reverse the list grows directly with the number of nodes.
[X] Wrong: "Reversing a doubly linked list takes more than linear time because of the two pointers per node."
[OK] Correct: Each node is visited once, and swapping two pointers is a constant time action, so overall time is still linear.
Understanding this helps you explain how linked list operations scale, a common topic in interviews.
"What if we tried to reverse the list using recursion instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes with pointers to both previous and next nodes, allowing traversal in both directions.Step 2: Purpose of reversing
Reversing swaps the previous and next pointers of each node, making the last node the new head and changing traversal direction.Final Answer:
To change the direction of traversal so the last node becomes the first -> Option AQuick Check:
Reversing swaps pointers to reverse traversal [OK]
- Thinking reversing deletes nodes
- Confusing reversing with sorting
- Assuming it converts to singly linked list
next and prev pointers of a node curr in Python?Solution
Step 1: Understand Python tuple unpacking
Usinga, b = b, aswaps values ofaandbsimultaneously without temporary variable.Step 2: Apply to pointers
Swappingcurr.nextandcurr.prevcorrectly usescurr.next, curr.prev = curr.prev, curr.next.Final Answer:
curr.next, curr.prev = curr.prev, curr.next -> Option BQuick Check:
Tuple unpacking swaps pointers correctly [OK]
- Assigning sequentially causing both to be same
- Not swapping both pointers
- Using incorrect order in assignment
Solution
Step 1: Original list traversal
The list is 1 <-> 2 <-> 3 <-> null, head at 1, tail at 3.Step 2: Reverse pointers
Swapping next and prev pointers reverses the list, making 3 the new head and 1 the new tail.Final Answer:
3 <-> 2 <-> 1 <-> null -> Option AQuick Check:
Reversed list order is 3, 2, 1 [OK]
- Confusing original order with reversed
- Misplacing null pointer
- Assuming singly linked list after reverse
def reverse(head):
curr = head
while curr:
curr.next = curr.prev
curr.prev = curr.next
curr = curr.prev
return headSolution
Step 1: Analyze pointer swapping
Assigningcurr.next = curr.prevthencurr.prev = curr.nextsequentially overwritescurr.nextbefore using it, losing original value.Step 2: Effect on traversal
Because pointers are not swapped simultaneously, traversal usingcurr = curr.prevfails and links become incorrect.Final Answer:
Pointers are not swapped simultaneously causing incorrect links -> Option CQuick Check:
Swap pointers simultaneously to avoid overwriting [OK]
- Swapping pointers in two separate assignments
- Returning old head instead of new head
- Incorrect loop condition
Solution
Step 1: Identify traversal direction after reversing
After reversing, the new head points to the start of the reversed list, and traversal should usenextpointers to move forward.Step 2: Correct printing loop
Starting fromreversed_head, printingcurr.dataand moving withcurr = curr.nextprints the list in correct order untilcurris null.Final Answer:
curr = reversed_head while curr: print(curr.data, end=' <-> ') curr = curr.next print('null') -> Option DQuick Check:
Traverse reversed list using next pointers from new head [OK]
- Using prev pointer to traverse forward
- Starting from original head after reverse
- Loop condition stopping early
Ready to prove it?
Pass the test and this pattern moves unknown
