Reverse a Doubly Linked List in DSA Go - Time & Space Complexity
We want to understand how the time needed to reverse a doubly linked list changes as the list grows.
How does the work increase when the list has more nodes?
Analyze the time complexity of the following code snippet.
func reverseDoublyLinkedList(head *Node) *Node {
current := head
var temp *Node
for current != nil {
temp = current.prev
current.prev = current.next
current.next = temp
current = current.prev
}
if temp != nil {
head = temp.prev
}
return head
}
This code reverses the links of a doubly linked list so the last node becomes the first.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that visits each node once. - How many times: Exactly once per node, so
ntimes for a list of sizen.
As the list gets longer, 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: Doubling the list size roughly doubles the work needed.
Time Complexity: O(n)
This means the time to reverse the list grows in a straight line with the number of nodes.
[X] Wrong: "Reversing a doubly linked list takes more than linear time because we change two pointers per node."
[OK] Correct: Changing two pointers per node is still a constant amount of work per node, so overall time grows linearly with the number of nodes.
Understanding this linear time reversal helps you handle linked list problems efficiently, a common skill interviewers look for.
"What if we changed the list to a singly linked list without backward pointers? 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.Step 2: Define reversing operation
Reversing means swapping the previous and next pointers for all nodes, so the last node becomes the first.Final Answer:
To make the last node the first and the first node the last -> Option CQuick Check:
Reverse = swap previous and next pointers [OK]
- Confusing reversing with sorting
- Thinking reversing deletes nodes
- Assuming reversing adds nodes
next and prev pointers of a node curr in a doubly linked list?Solution
Step 1: Understand multiple assignment in Go
Go allows swapping two variables in one line using multiple assignment.Step 2: Identify correct swap syntax
The correct way to swap iscurr.next, curr.prev = curr.prev, curr.nextwhich exchanges the values.Final Answer:
curr.next, curr.prev = curr.prev, curr.next -> Option AQuick Check:
Swap pointers with multiple assignment [OK]
- Assigning one pointer twice without swapping
- Using separate lines causing overwrite
- Not swapping pointers at all
type Node struct {
data int
prev *Node
next *Node
}
func reverse(head *Node) *Node {
var temp *Node
curr := head
for curr != nil {
temp = curr.prev
curr.prev = curr.next
curr.next = temp
curr = curr.prev
}
if temp != nil {
head = temp.prev
}
return head
}
// List before reversal: 1 <-> 2 <-> 3 <-> nil
// After reversal, print from new head
func printList(head *Node) {
curr := head
for curr != nil {
fmt.Print(curr.data, " <-> ")
curr = curr.next
}
fmt.Println("nil")
}Solution
Step 1: Trace pointer swaps in the loop
Each node swaps its prev and next pointers, moving curr to original next node via curr.prev after swap.Step 2: Update head to last processed node
After loop, head is set to temp.prev which points to the new first node (original last node).Final Answer:
3 <-> 2 <-> 1 <-> nil -> Option BQuick Check:
Reversed list prints nodes in reverse order [OK]
- Not updating head after reversal
- Confusing prev and next pointers
- Printing original list without reversal
func reverse(head *Node) *Node {
curr := head
for curr != nil {
curr.prev, curr.next = curr.next, curr.prev
curr = curr.next
}
return head
}Solution
Step 1: Analyze pointer swapping and traversal
The pointers are swapped correctly, but after swapping, curr.next points to the previous node, so moving curr to curr.next moves backward.Step 2: Identify correct traversal direction
After swapping, curr should move to curr.prev (original next) to continue forward traversal.Final Answer:
The loop moves curr to curr.next instead of curr.prev after swapping -> Option AQuick Check:
Traverse using curr.prev after swap [OK]
- Moving curr in wrong direction after swap
- Not updating head to new first node
- Swapping pointers incorrectly
Solution
Step 1: Reverse the doubly linked list
Use pointer swapping to reverse the list so the order is changed.Step 2: Traverse from new head and sum nodes at even positions
After reversal, positions change, so summing even positions requires traversal from new head counting nodes.Final Answer:
First reverse the list, then traverse from new head summing nodes at even positions -> Option DQuick Check:
Reverse first, then sum even positions [OK]
- Summing before reversal causing wrong positions
- Summing odd positions instead of even
- Ignoring reversal effect on node order
Ready to prove it?
Pass the test and this pattern moves unknown
