What if you could flip a list instantly without moving a single item?
Why Reverse a Doubly Linked List in DSA Go?
Imagine you have a list of your favorite songs saved in order. Now, you want to play them backward, from last to first. Doing this by hand means rewriting the entire list in reverse, which takes time and can cause mistakes.
Manually reversing a list means moving each item one by one to a new list in reverse order. This is slow and easy to mess up, especially if the list is long. You might lose track or accidentally skip songs.
Reversing a doubly linked list swaps the direction of the links between nodes. Instead of moving data around, we just change pointers, making the process fast and safe. This way, the list order flips instantly without copying or losing items.
for i := len(list) - 1; i >= 0; i-- { reversedList = append(reversedList, list[i]) }
func reverseDoublyLinkedList(head *Node) *Node {
var temp *Node
current := head
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 lets you quickly flip the order of items in a list without extra space or copying, making your programs faster and more efficient.
Music players use this to reverse playlists instantly when you choose the "play backward" option, without creating a new list or waiting.
Manual reversal is slow and error-prone.
Reversing pointers in a doubly linked list is fast and safe.
This technique saves time and memory in many applications.
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
