Bird
Raised Fist0
DSA Javascriptprogramming~15 mins

Reverse a Doubly Linked List in DSA Javascript - Deep Dive

Choose your learning style10 modes available
Overview - Reverse a Doubly Linked List
What is it?
A doubly linked list is a chain of nodes where each node points to both its previous and next nodes. Reversing it means changing the direction so the last node becomes the first, and the first becomes the last. This operation swaps the links so you can traverse the list backward as if it were forward. It helps in situations where you want to process data in reverse order efficiently.
Why it matters
Without the ability to reverse a doubly linked list, you would need extra memory or complex logic to process data backward. Reversing in place saves time and space, making programs faster and more efficient. This is important in real-world apps like music playlists, undo-redo features, or navigation history where reversing order quickly is needed.
Where it fits
Before learning this, you should understand what linked lists are, especially doubly linked lists and how nodes connect. After mastering reversal, you can explore more complex list operations like sorting, merging, or advanced data structures like trees and graphs that build on linked list concepts.
Mental Model
Core Idea
Reversing a doubly linked list means swapping each node's previous and next links so the list direction flips without creating new nodes.
Think of it like...
Imagine a two-way street where cars can go forward or backward. Reversing the list is like flipping the street signs so cars that used to go east now go west, and vice versa, but the road itself stays the same.
Original list:
Head ⇄ Node1 ⇄ Node2 ⇄ Node3 ⇄ Tail

After reversal:
Tail ⇄ Node3 ⇄ Node2 ⇄ Node1 ⇄ Head
Build-Up - 6 Steps
1
FoundationUnderstanding Doubly Linked List Structure
🤔
Concept: Learn how each node in a doubly linked list has two pointers: one to the next node and one to the previous node.
A doubly linked list node looks like this: class Node { constructor(value) { this.value = value; this.next = null; // points to next node this.prev = null; // points to previous node } } Nodes connect so you can move forward or backward through the list.
Result
You can move from any node to its next or previous node easily.
Understanding the two pointers per node is key because reversing means swapping these pointers, not just changing values.
2
FoundationTraversing a Doubly Linked List
🤔
Concept: Learn how to move through the list from head to tail and tail to head using next and prev pointers.
To traverse forward: let current = head; while (current !== null) { console.log(current.value); current = current.next; } To traverse backward: let current = tail; while (current !== null) { console.log(current.value); current = current.prev; }
Result
You can print all node values in order or reverse order.
Knowing traversal directions helps visualize what reversal changes: it flips these directions.
3
IntermediateSwapping Next and Prev Pointers
🤔Before reading on: do you think reversing means changing node values or swapping pointers? Commit to your answer.
Concept: Reversing a doubly linked list means swapping each node's next and prev pointers, not changing the node values.
For each node: - Temporarily store next pointer - Swap next and prev pointers - Move to the original next node (which is now prev after swap) This changes the direction of links without touching values.
Result
Each node's next and prev pointers are swapped, reversing the list direction.
Understanding that reversal is about pointer swapping, not data swapping, saves time and memory.
4
IntermediateUpdating Head After Reversal
🤔Before reading on: after swapping pointers, which node becomes the new head? Commit to your answer.
Concept: After swapping pointers, the original tail node becomes the new head of the reversed list.
While swapping pointers, keep track of the last node processed. After the loop ends, this node is the new head. Return or update the head pointer to this node.
Result
The list head points to the original tail, completing the reversal.
Tracking the new head during reversal is crucial to access the reversed list correctly.
5
AdvancedComplete JavaScript Reversal Function
🤔Before reading on: do you think the reversal function needs extra memory or can work in place? Commit to your answer.
Concept: Implement the reversal in place without extra memory by swapping pointers and updating head.
function reverseDoublyLinkedList(head) { let current = head; let temp = null; while (current !== null) { // Swap next and prev temp = current.prev; current.prev = current.next; current.next = temp; // Move to next node (originally next, now prev) current = current.prev; } // After loop, temp is at the old head's prev, so new head is temp.prev if (temp !== null) { head = temp.prev; } return head; } // Example usage: // List: 1 ⇄ 2 ⇄ 3 // After reversal: 3 ⇄ 2 ⇄ 1
Result
The function returns the new head of the reversed list, with all pointers swapped correctly.
Knowing how to reverse in place avoids extra space and keeps the operation efficient.
6
ExpertHandling Edge Cases and Performance
🤔Before reading on: do you think reversing an empty or single-node list changes anything? Commit to your answer.
Concept: Consider empty lists and single-node lists where reversal is trivial, and understand time complexity is linear.
Edge cases: - Empty list (head is null): return null immediately. - Single node: swapping pointers does nothing, return node. Performance: - Each node is visited once, so time complexity is O(n). - No extra memory used, space complexity is O(1). This makes reversal efficient for large lists.
Result
Reversal works correctly and efficiently for all list sizes.
Recognizing edge cases prevents bugs and understanding complexity helps optimize real-world applications.
Under the Hood
Internally, each node stores two memory addresses: one for the next node and one for the previous node. Reversing swaps these addresses for every node. The traversal pointer moves through the list by following the original next pointers, but after swapping, it follows the original prev pointers. The head pointer is updated to the last node processed, which was originally the tail. This pointer manipulation changes the direction of the list without moving or copying node data.
Why designed this way?
Doubly linked lists were designed to allow easy forward and backward traversal. Reversing by swapping pointers is efficient because it avoids creating new nodes or copying data. Alternatives like creating a new reversed list would use extra memory and time. This in-place reversal respects memory constraints and performance needs, especially important in systems with limited resources.
Start:
Head -> Node1 ⇄ Node2 ⇄ Node3 ← Tail

Process Node1:
Swap next and prev
Node1.prev ← Node2
Node1.next ← null
Move to Node2 (original next)

Process Node2:
Swap next and prev
Node2.prev ← Node3
Node2.next ← Node1
Move to Node3

Process Node3:
Swap next and prev
Node3.prev ← null
Node3.next ← Node2

End:
New Head -> Node3 ⇄ Node2 ⇄ Node1 ← New Tail
Myth Busters - 4 Common Misconceptions
Quick: Does reversing a doubly linked list mean swapping node values? Commit to yes or no.
Common Belief:Reversing means swapping the values inside the nodes to reverse the order.
Tap to reveal reality
Reality:Reversing swaps the next and prev pointers of each node, not the values inside nodes.
Why it matters:Swapping values is inefficient and unnecessary; pointer swapping is faster and uses less memory.
Quick: After reversal, does the original head remain the head? Commit to yes or no.
Common Belief:The head of the list stays the same after reversal.
Tap to reveal reality
Reality:The original tail becomes the new head after reversal.
Why it matters:Failing to update the head pointer leads to incorrect list traversal and bugs.
Quick: Is extra memory always needed to reverse a doubly linked list? Commit to yes or no.
Common Belief:You must create a new list or use extra memory to reverse a doubly linked list.
Tap to reveal reality
Reality:Reversal can be done in place by swapping pointers without extra memory.
Why it matters:Using extra memory wastes resources and slows down the program unnecessarily.
Quick: Does reversing a doubly linked list change the order of nodes in memory? Commit to yes or no.
Common Belief:Reversing rearranges nodes physically in memory.
Tap to reveal reality
Reality:Nodes stay in the same memory locations; only pointers change.
Why it matters:Misunderstanding this can lead to incorrect assumptions about memory usage and performance.
Expert Zone
1
Swapping pointers must be done carefully to avoid losing track of nodes; using a temporary variable is essential.
2
The reversal process inherently reverses traversal direction, which can affect algorithms relying on original order.
3
In some languages or environments, pointer manipulation can cause subtle bugs if references are not handled properly.
When NOT to use
Avoid reversing doubly linked lists in place if you need to preserve the original list order for other operations. Instead, create a new reversed copy. Also, for singly linked lists, this method does not apply; different techniques are needed.
Production Patterns
In real systems, reversing doubly linked lists is used in undo-redo stacks, browser history navigation, and playlist management where quick reversal of order is needed without extra memory overhead.
Connections
Singly Linked List Reversal
Similar operation but with only one pointer per node, requiring different pointer manipulation.
Understanding doubly linked list reversal clarifies the extra complexity and flexibility added by having two pointers.
Stack Data Structure
Reversing a list is conceptually similar to pushing all elements onto a stack and popping them to reverse order.
Knowing this connection helps understand how stacks can be used to reverse sequences in algorithms.
DNA Double Helix Structure
Both have two strands connected in opposite directions, and reversing one strand changes the orientation.
This biological analogy shows how directionality and pairing are fundamental concepts across fields.
Common Pitfalls
#1Not updating the head pointer after reversal.
Wrong approach:function reverse(head) { let current = head; let temp = null; while (current !== null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } return head; // head not updated }
Correct approach:function reverse(head) { let current = head; let 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; }
Root cause:Forgetting that the head changes to the original tail after reversal causes incorrect list access.
#2Swapping node values instead of pointers.
Wrong approach:function reverse(head) { let current = head; let values = []; while (current !== null) { values.push(current.value); current = current.next; } current = head; for (let i = values.length - 1; i >= 0; i--) { current.value = values[i]; current = current.next; } return head; }
Correct approach:function reverse(head) { let current = head; let 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; }
Root cause:Confusing data reversal with pointer reversal leads to inefficient and unnecessary operations.
#3Not handling empty or single-node lists.
Wrong approach:function reverse(head) { let current = head; let temp = null; while (current !== null) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } head = temp.prev; return head; } // No check for null head or single node
Correct approach:function reverse(head) { if (head === null || head.next === null) return head; let current = head; let 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; }
Root cause:Ignoring edge cases causes runtime errors or incorrect results.
Key Takeaways
A doubly linked list has nodes with two pointers: next and prev, allowing forward and backward traversal.
Reversing a doubly linked list means swapping each node's next and prev pointers to flip the list direction.
The original tail becomes the new head after reversal, so updating the head pointer is essential.
Reversal can be done in place without extra memory, making it efficient for large lists.
Handling edge cases like empty or single-node lists prevents bugs and ensures correctness.

Practice

(1/5)
1. What does reversing a doubly linked list mean?
easy
A. Adding a new node at the end
B. Deleting all nodes from the list
C. Sorting the nodes in ascending order
D. Swapping the next and previous pointers of each node

Solution

  1. Step 1: Understand doubly linked list structure

    Each node has two pointers: one to the next node and one to the previous node.
  2. Step 2: Reverse means swapping pointers

    Reversing means changing each node's next pointer to previous and previous pointer to next.
  3. Final Answer:

    Swapping the next and previous pointers of each node -> Option D
  4. Quick Check:

    Reverse = swap next and prev pointers [OK]
Hint: Reverse means swap next and prev pointers [OK]
Common Mistakes:
  • Thinking reverse means deleting nodes
  • Confusing reverse with sorting
  • Adding nodes instead of reversing pointers
2. Which of the following is the correct way to swap the next and prev pointers of a node in JavaScript?
easy
A. node.prev = node.next; node.next = node.prev;
B. node.next = node.prev; node.prev = node.next;
C. let temp = node.next; node.next = node.prev; node.prev = temp;
D. node.next = node.next; node.prev = node.prev;

Solution

  1. Step 1: Use a temporary variable to hold one pointer

    Store node.next in temp to avoid losing its value.
  2. Step 2: Swap pointers correctly

    Assign node.next = node.prev, then node.prev = temp to complete swap.
  3. Final Answer:

    let temp = node.next; node.next = node.prev; node.prev = temp; -> Option C
  4. Quick Check:

    Swap with temp variable = correct [OK]
Hint: Use temp variable to swap pointers safely [OK]
Common Mistakes:
  • Swapping without temp loses pointer data
  • Assigning both pointers to same value
  • Not swapping pointers at all
3. What will be the output list after reversing this doubly linked list?
Head -> 1 <-> 2 <-> 3 <-> null

After reversal, what is the new head node's value?
medium
A. 3
B. 1
C. 2
D. null

Solution

  1. Step 1: Original list order

    List is 1 <-> 2 <-> 3, head points to 1.
  2. Step 2: Reverse swaps pointers, so last node becomes head

    After reversal, 3 becomes the new head, pointing backward to 2 and forward to null.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    New head after reverse = last node = 3 [OK]
Hint: New head after reverse is original tail [OK]
Common Mistakes:
  • Assuming head stays same after reverse
  • Confusing node values order
  • Thinking null becomes head
4. Identify the bug in this reverse function for a doubly linked list:
function reverse(head) {
  let current = head;
  while (current != null) {
    let temp = current.next;
    current.next = current.prev;
    current.prev = temp;
    current = current.next;
  }
  return head;
}
medium
A. The function does not update the head to the new first node
B. Swapping next and prev pointers is done incorrectly
C. The loop condition should be current.next != null
D. The function should return null instead of head

Solution

  1. Step 1: Check pointer swapping

    Swapping next and prev pointers is done correctly using temp variable.
  2. Step 2: Check head update after reversal

    Function returns original head, but after reversal head should be last node visited, so head is not updated.
  3. Final Answer:

    The function does not update the head to the new first node -> Option A
  4. Quick Check:

    Head must update to new first node [OK]
Hint: Update head to last processed node after loop [OK]
Common Mistakes:
  • Not updating head after reversal
  • Incorrect loop condition causing infinite loop
  • Returning wrong node at end
5. Given a doubly linked list with nodes:
Head -> 10 <-> 20 <-> 30 <-> 40 <-> null

After reversing the list, what is the correct sequence of nodes starting from the new head?
hard
A. 10 <-> 20 <-> 30 <-> 40 <-> null
B. 40 <-> 30 <-> 20 <-> 10 <-> null
C. 40 <-> 20 <-> 30 <-> 10 <-> null
D. null <-> 40 <-> 30 <-> 20 <-> 10

Solution

  1. Step 1: Original list order

    Nodes are linked as 10 <-> 20 <-> 30 <-> 40, head at 10.
  2. Step 2: Reverse swaps pointers and changes head to last node

    After reversal, head is 40, and nodes link backward as 40 <-> 30 <-> 20 <-> 10.
  3. Final Answer:

    40 <-> 30 <-> 20 <-> 10 <-> null -> Option B
  4. Quick Check:

    Reverse list order = original tail to head [OK]
Hint: Reversed list order is original list backwards [OK]
Common Mistakes:
  • Not reversing the order correctly
  • Mixing node values in wrong order
  • Confusing null placement in list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard