What if you could flip a long chain of items instantly without breaking a sweat?
Why Reverse a Doubly Linked List in DSA C#?
Imagine you have a paper chain with links connected in order. You want to flip the chain so the last link becomes the first and the first becomes the last. Doing this by hand means unhooking and rehooking each link one by one.
Manually flipping each link is slow and easy to mess up. You might lose track of connections or break the chain. It takes a lot of time and careful work, especially if the chain is very long.
Reversing a doubly linked list swaps the direction of links automatically by changing pointers. This way, the last element becomes the first without breaking the chain or needing extra space. It's fast and safe.
Node current = head; while (current.next != null) { // manually detach and reattach links current = current.next; }
Node current = head; Node temp = null; while (current != null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp != null) head = temp.prev;
This lets you quickly reverse the order of elements in a list, enabling easy backward traversal and new ways to process data.
Think of a music playlist where you want to play songs in reverse order without creating a new list. Reversing the doubly linked list of songs makes this simple and efficient.
Manual reversal is slow and error-prone.
Reversing swaps pointers to flip the list quickly.
Useful for backward traversal and data reordering.
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected forward and backward using next and prev pointers.Step 2: Purpose of reversing
Reversing swaps the direction so the last node becomes the first and the first becomes the last.Final Answer:
To change the order of nodes so the last becomes first and vice versa -> Option AQuick Check:
Reversing = changing node order [OK]
- Confusing reversing with deleting nodes
- Thinking reversing sorts the list
- Mixing up adding nodes with reversing
next and prev pointers of a node in a doubly linked list?Solution
Step 1: Understand swapping pointers
To swap two pointers, use a temporary variable to hold one value before overwriting it.Step 2: Check each option
The correct snippet saves node.next in temp, assigns node.next = node.prev, then node.prev = temp. Direct assignments without temp cause data loss. The flawed temp usage assigns node.next = node.prev without saving the original node.next first, losing data.Final Answer:
var temp = node.next; node.next = node.prev; node.prev = temp; -> Option DQuick Check:
Use temp variable to swap pointers [OK]
- Swapping without a temporary variable
- Assigning pointers in wrong order
- Confusing next and prev roles
class Node {
public int data;
public Node next, prev;
public Node(int d) { data = d; next = prev = null; }
}
// List: 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second; second.prev = head;
second.next = third; third.prev = second;
// Reverse function
Node Reverse(Node head) {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) head = temp.prev;
return head;
}
head = Reverse(head);
// Print list forward
Node tempNode = head;
string result = "";
while (tempNode != null) {
result += tempNode.data + " -> ";
tempNode = tempNode.next;
}
result += "null";
Console.WriteLine(result);Solution
Step 1: Trace the reversal loop
Each node swaps its next and prev pointers. The loop moves current to current.prev (which was originally next). After the loop, head is updated to the last processed node.Step 2: Verify the new order
Original list: 1 <-> 2 <-> 3. After reversal, the list becomes 3 <-> 2 <-> 1. Printing forward from new head prints 3 -> 2 -> 1 -> null.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Reversed list prints backward order [OK]
- Not updating head after reversal
- Confusing next and prev pointers
- Printing list without updating head
Node Reverse(Node head) {
Node current = head;
Node temp = null;
while (current != null) {
temp = current.next;
current.next = current.prev;
current.prev = temp;
current = current.next;
}
if (temp != null) head = temp.prev;
return head;
}Solution
Step 1: Analyze pointer swapping
The code swaps next and prev pointers correctly, but moves current to current.next, which after swapping points backward.Step 2: Correct traversal direction
After swapping, current should move to current.prev (originally next) to continue forward traversal. Moving to current.next causes skipping or infinite loop.Final Answer:
The loop should move current to current.prev, not current.next -> Option BQuick Check:
Traverse using swapped pointers correctly [OK]
- Moving current in wrong direction after swap
- Incorrect temp assignment
- Wrong head update after reversal
Solution
Step 1: Initialize a counter before the loop
Start a counter at zero to track how many nodes are processed during reversal.Step 2: Increment counter inside the reversal loop
Each time a node is processed (pointers swapped), increase the counter by one.Step 3: Return both new head and count
Modify the function to return the new head and the count of reversed nodes.Final Answer:
Add a counter variable initialized to 0, increment it inside the reversal loop, and return it along with the new head. -> Option CQuick Check:
Count nodes during reversal loop [OK]
- Counting nodes before reversal only
- Incrementing counter after loop ends
- Not returning the count with the head
Ready to prove it?
Pass the test and this pattern moves unknown
