📖 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 reverse the order of songs to listen from the last added to the first.
🎯 Goal: Build a program that creates a doubly linked list of songs, then reverses the list so the last song becomes the first, and prints the reversed list.
📋 What You'll Learn
Create a doubly linked list with exactly these songs in order: "SongA", "SongB", "SongC", "SongD"
Create a variable called head that points to the first node of the list
Write a method called reverseList that reverses the doubly linked list starting from head
Print the reversed list starting from the new head, showing song names separated by " <-> " and ending with "null"
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps to allow easy forward and backward navigation through items.
💼 Career
Understanding how to manipulate doubly linked lists is important for software developers working on data structures, system design, and performance optimization.
Progress0 / 4 steps
1
Create the doubly linked list with songs
Create a class called Node with String song, Node next, and Node prev. Then create four nodes with songs "SongA", "SongB", "SongC", and "SongD". Link them to form a doubly linked list in this order. Finally, create a variable called head that points to the first node.
DSA Java
Hint
Remember to link each node's next and prev pointers correctly to form the doubly linked list.
2
Add a method to reverse the doubly linked list
Inside the Main class, add a static method called reverseList that takes a Node head parameter and returns the new head after reversing the list. The method should swap the next and prev pointers for each node while traversing the list.
DSA Java
Hint
Swap the next and prev pointers for each node. Move forward using the old prev pointer after swapping.
3
Call the reverse method and update head
In the main method, call the reverseList method with head as argument and assign the result back to head.
DSA Java
Hint
Assign the result of reverseList(head) back to head to update the list start.
4
Print the reversed doubly linked list
Add a printList static method in the Main class that takes Node head and prints the songs in order separated by " <-> ", ending with "null". Then call printList(head) in main after reversing the list.
DSA Java
Hint
Traverse from head using next and print each song followed by " <-> ". End with "null".
Practice
(1/5)
1. What is the main purpose of reversing a doubly linked list?
easy
A. To change the direction of traversal so the last node becomes the first
B. To delete all nodes from the list
C. To sort the nodes in ascending order
D. To convert it into a singly linked list
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 first and vice versa.
Final Answer:
To change the direction of traversal so the last node becomes the first -> Option A
Quick Check:
Reversing means swapping directions [OK]
Hint: Reversing flips the list direction, last becomes first [OK]
Common Mistakes:
Confusing reversing with deleting nodes
Thinking reversing sorts the list
Assuming it converts to singly linked list
2. Which of the following Java code snippets correctly swaps the next and prev pointers of a node curr in a doubly linked list?
A. The pointers next and prev are not swapped correctly
B. The temp variable should store curr.prev, not curr.next
C. The loop moves curr to curr.next instead of curr.prev after swapping
D. The loop condition should be curr.next != null
Solution
Step 1: Analyze pointer swapping
The code swaps next and prev correctly using temp.
Step 2: Check loop iteration step
After swapping, curr.next points to the original prev. To move forward in the original list, curr should move to curr.prev (which is original next). The code incorrectly moves to curr.next, causing wrong traversal.
Final Answer:
The loop moves curr to curr.next instead of curr.prev after swapping -> Option C
Quick Check:
Move curr to curr.prev after swapping [OK]
Hint: After swapping, move curr to curr.prev to continue traversal [OK]
Common Mistakes:
Moving curr to wrong pointer after swap
Confusing temp usage
Incorrect loop condition
5. You have a doubly linked list with nodes containing integer data. After reversing the list using the standard pointer swap method, you want to find the middle node efficiently. Which approach is best to find the middle node after reversal?
hard
A. Use two pointers starting from the new head: one moves one step, the other moves two steps until the faster reaches null
B. Traverse from the original head before reversal to find the middle
C. Count nodes from the tail and divide by two to find middle index
D. Reverse the list again and then find the middle node
Solution
Step 1: Understand list reversal effect
After reversal, the head points to the original tail. The list is still doubly linked and can be traversed forward.
Step 2: Efficient middle node finding
The two-pointer technique (slow and fast pointers) starting from the new head finds the middle in one pass efficiently.
Final Answer:
Use two pointers starting from the new head: one moves one step, the other moves two steps until the faster reaches null -> Option A
Quick Check:
Two-pointer method finds middle in O(n) [OK]
Hint: Use slow and fast pointers from new head to find middle [OK]
Common Mistakes:
Trying to find middle from old head after reversal
Counting nodes from tail which is inefficient
Reversing list again unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.