📖 Scenario: You are working on a music playlist app. The playlist is stored as a doubly linked list where each node holds a song name. Sometimes, users want to play the playlist in reverse order.
🎯 Goal: Build a doubly linked list with 3 songs, then reverse the list by swapping the links, and finally print the reversed playlist.
📋 What You'll Learn
Create a doubly linked list with exactly 3 nodes containing songs: "SongA", "SongB", "SongC" in that order
Create a variable called current to help traverse the list
Write code to reverse the doubly linked list by swapping the next and prev pointers of each node
Print the reversed list starting from the new head node
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history navigation), and many applications where you need to move forward and backward through items.
💼 Career
Understanding linked lists and how to reverse them is a common interview question and helps build a strong foundation for working with pointers and dynamic data structures.
Progress0 / 4 steps
1
Create the doubly linked list with 3 songs
Create a struct type called Node with fields song string, prev *Node, and next *Node. Then create 3 nodes named node1, node2, and node3 with songs "SongA", "SongB", and "SongC" respectively. Link them so that node1.next = node2, node2.prev = node1, node2.next = node3, and node3.prev = node2. Finally, create a variable head pointing to node1.
DSA Go
Hint
Define the Node struct first. Then create nodes with &Node{song: "SongName"}. Link nodes by setting next and prev pointers. Finally, set head to the first node.
2
Create a variable to traverse the list
Create a variable called current and set it equal to head to start traversing the list from the beginning.
DSA Go
Hint
Set current equal to head so you can start from the first node.
3
Reverse the doubly linked list
Write a for loop that runs while current != nil. Inside the loop, swap current.next and current.prev. Then move current to the new prev (which was originally next). After the loop, update head to the last non-nil current node.
DSA Go
Hint
Inside the loop, swap prev and next pointers for each node. Move current to the old next which is now prev. After the loop, set head to the last node processed.
4
Print the reversed doubly linked list
Use a for loop with a variable temp starting at head. In each iteration, print temp.song followed by " -> ". Move temp to temp.next. After the loop, print "nil" to mark the end.
DSA Go
Hint
Start from head and print each song followed by " -> ". Stop when you reach the end and print "nil".
Practice
(1/5)
1. What is the main purpose of reversing a doubly linked list?
easy
A. To sort the list in ascending order
B. To delete all nodes from the list
C. To make the last node the first and the first node the last
D. To add a new node at the end
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 C
Quick Check:
Reverse = swap previous and next pointers [OK]
Hint: Reversing swaps next and previous pointers [OK]
Common Mistakes:
Confusing reversing with sorting
Thinking reversing deletes nodes
Assuming reversing adds nodes
2. Which of the following Go code snippets correctly swaps the next and prev pointers of a node curr in a doubly linked list?
easy
A. curr.next, curr.prev = curr.prev, curr.next
B. curr.next = curr.prev; curr.prev = curr.next
C. curr.prev = curr.next; curr.next = curr.prev
D. curr.next = curr.next; curr.prev = curr.prev
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 is curr.next, curr.prev = curr.prev, curr.next which exchanges the values.
Final Answer:
curr.next, curr.prev = curr.prev, curr.next -> Option A
Quick Check:
Swap pointers with multiple assignment [OK]
Hint: Use Go's multiple assignment to swap pointers [OK]
Common Mistakes:
Assigning one pointer twice without swapping
Using separate lines causing overwrite
Not swapping pointers at all
3. Given the following Go code to reverse a doubly linked list, what will be the printed output after reversal?
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")
}
medium
A. 1 <-> 2 <-> 3 <-> nil
B. 3 <-> 2 <-> 1 <-> nil
C. nil
D. 1 <-> 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 B
Quick Check:
Reversed list prints nodes in reverse order [OK]
Hint: Reversed list prints nodes backward [OK]
Common Mistakes:
Not updating head after reversal
Confusing prev and next pointers
Printing original list without reversal
4. Identify the bug in this Go function that attempts to reverse a doubly linked list:
func reverse(head *Node) *Node {
curr := head
for curr != nil {
curr.prev, curr.next = curr.next, curr.prev
curr = curr.next
}
return head
}
medium
A. The loop moves curr to curr.next instead of curr.prev after swapping
B. The pointers are not swapped correctly
C. The function does not return the new head
D. The function deletes nodes accidentally
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 A
Quick Check:
Traverse using curr.prev after swap [OK]
Hint: After swap, move curr to curr.prev, not curr.next [OK]
Common Mistakes:
Moving curr in wrong direction after swap
Not updating head to new first node
Swapping pointers incorrectly
5. You have a doubly linked list with nodes containing integers. After reversing it using the standard pointer swap method, you want to find the sum of all nodes at even positions (2nd, 4th, etc.) in the reversed list. Which approach correctly combines reversal and summation?
hard
A. Sum all nodes without reversal
B. Sum nodes at even positions before reversal, then reverse the list
C. Reverse the list and sum nodes at odd positions instead
D. First reverse the list, then traverse from new head summing nodes at even positions
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 D
Quick Check:
Reverse first, then sum even positions [OK]
Hint: Reverse first, then sum nodes by position [OK]
Common Mistakes:
Summing before reversal causing wrong positions
Summing odd positions instead of even
Ignoring reversal effect on node order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.