📖 Scenario: You are working on a music playlist app. The playlist is stored as a doubly linked list where each node contains a song name. Sometimes, users want to reverse the order of songs in their playlist.
🎯 Goal: Build a program that creates a doubly linked list with 3 songs, then reverses the list, and finally prints the reversed playlist.
📋 What You'll Learn
Create a doubly linked list with exactly 3 nodes containing the songs: "Song1", "Song2", "Song3" in that order
Create a pointer variable called head that points to the first node
Create a pointer variable called temp for swapping nodes during reversal
Write a function called reverseList that reverses the doubly linked list by swapping the next and prev pointers of each node
Print the reversed list starting from the new head using the printList function
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps where users navigate forward and backward through items.
💼 Career
Understanding linked lists and how to reverse them is a common interview question and helps in managing complex data structures efficiently.
Progress0 / 4 steps
1
Create the doubly linked list with 3 songs
Create a struct called Node with char* data, Node* next, and Node* prev. Then create 3 nodes with data "Song1", "Song2", and "Song3". Link them to form a doubly linked list. Create a pointer head that points to the first node.
DSA C
Hint
Remember to link the nodes both ways: next and prev.
2
Add a pointer variable temp for swapping
Inside main, declare a pointer variable called temp of type Node* and initialize it to NULL.
DSA C
Hint
Declare Node* temp = NULL; inside main.
3
Write the reverseList function to reverse the doubly linked list
Write a function called reverseList that takes a pointer head of type Node* and returns the new head after reversing the list. Inside the function, use a pointer temp to swap the next and prev pointers of each node. Update head to the last node after reversal. Call reverseList from main and update head.
DSA C
Hint
Swap next and prev for each node. Move current to prev after swapping. Update head to last node.
4
Print the reversed doubly linked list
Write a function called printList that takes head and prints the list data separated by arrows like: Song3 <-> Song2 <-> Song1. Call printList(head) from main after reversing the list.
DSA C
Hint
Traverse from head and print each node's data. Print <-> between nodes.
Practice
(1/5)
1. What is the main purpose of reversing a doubly linked list?
easy
A. To convert it into a singly linked list
B. To delete all nodes from the list
C. To make the last node the first and the first node the last
D. To sort the list in ascending order
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 direction so the last node becomes the first and vice versa.
Final Answer:
To make the last node the first and the first node the last -> Option C
Quick Check:
Reverse = last becomes first [OK]
Hint: Reversing flips start and end nodes [OK]
Common Mistakes:
Thinking reversing deletes nodes
Confusing reversing with sorting
Assuming it changes list type
2. Which of the following is the correct way to swap the next and prev pointers of a node in C?
easy
A. temp = node->prev; node->prev = node->next; node->next = node->next;
B. node->next = node->prev; node->prev = node->next;
C. node->prev = node->next; node->next = node->prev;
D. temp = node->next; node->next = node->prev; node->prev = temp;
Solution
Step 1: Use a temporary variable to hold one pointer
Store node->next in temp to avoid losing its value.
Step 2: Swap pointers correctly
Assign node->next to node->prev, then node->prev to temp.
A. temp should store current->prev instead of current->next
B. current should move to current->next instead of current->prev
C. The head pointer update is incorrect
D. No error, code is correct
Solution
Step 1: Verify pointer swaps
The next/prev swaps correctly reverse directions as current traverses the original next chain (now in prev).
Step 2: Analyze temp assignment
Temp should store current->prev before swapping, not current->next, to avoid losing pointer.
Final Answer:
temp should store current->prev instead of current->next -> Option A
Quick Check:
Correct temp assignment preserves traversal [OK]
Hint: Temp must hold current->prev before swapping pointers [OK]
Common Mistakes:
Incorrect head pointer update logic
Moving current incorrectly after swap
Not updating head pointer properly
5. You have a doubly linked list with nodes containing integer data. After reversing the list using the standard pointer swap method, which of the following statements is TRUE about the list's traversal?
hard
A. Traversing from the new head using next pointers visits nodes in original reverse order
B. Traversing from the new head using prev pointers visits nodes in original order
C. Traversing from the original head using next pointers visits nodes in reversed order
D. Traversing from the new head using next pointers visits nodes in original order
Solution
Step 1: Understand reversal effect on pointers
Reversing swaps next and prev pointers, so next pointers now point backward in original list.
Step 2: Analyze traversal from new head
Starting at new head, following next pointers visits nodes in original reverse order.
Final Answer:
Traversing from the new head using next pointers visits nodes in original reverse order -> Option A
Quick Check:
Next pointers after reverse = original prev [OK]
Hint: Next pointers after reverse follow original prev links [OK]
Common Mistakes:
Assuming next pointers still go forward after reverse
Confusing traversal directions
Thinking original head remains start
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.