Bird
Raised Fist0
DSA Javascriptprogramming

Reverse a Doubly Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
To reverse a doubly linked list, we swap the next and previous links of each node, then move forward using the old next link.
Analogy: Imagine walking backwards on a path where each step has a forward and backward arrow; reversing means swapping these arrows so you can walk the path in the opposite direction.
null ← 1 ↔ 2 ↔ 3 -> null
↑head
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, reverse the list
Goal: Reverse the list so the order becomes 3 ↔ 2 ↔ 1
Step 1: Start at head (node 1), swap next and prev pointers
null -> 1 ← 2 ↔ 3 -> null
↑curr at 1
Why: Swapping pointers reverses the direction of links for current node
Step 2: Move curr to old next (node 2 after swap)
null ← 1 -> 2 ↔ 3 -> null
↑curr at 2
Why: After swapping, old next (now prev) is the next node to process
Step 3: Swap next and prev pointers of node 2
null ← 1 -> 2 ← 3 -> null
↑curr at 2
Why: Reverse links for node 2 to continue reversing the list
Step 4: Move curr to old next (node 3)
null ← 1 -> 2 -> 3 -> null
↑curr at 3
Why: Continue to next node using old next (now prev) pointer
Step 5: Swap next and prev pointers of node 3
null ← 1 -> 2 -> 3 ← null
↑curr at 3
Why: Reverse links for last node
Step 6: Move curr to old next (null), stop and update head to last processed node (3)
null ← 1 -> 2 -> 3 ← null
↑head at 3
Why: No more nodes to process, head must point to new first node
Result:
null ← 1 -> 2 -> 3 ← null
head at 3
List reversed: 3 ↔ 2 ↔ 1
Annotated Code
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
  }

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let curr = this.head;
    while (curr.next) {
      curr = curr.next;
    }
    curr.next = newNode;
    newNode.prev = curr;
  }

  reverse() {
    let curr = this.head;
    let temp = null;
    while (curr) {
      // Swap next and prev
      temp = curr.prev;
      curr.prev = curr.next;
      curr.next = temp;

      // Move curr to old next (which is now curr.prev after swap)
      curr = curr.prev;
    }
    // After loop, temp points to second-to-last node; temp.prev is new head
    if (temp) {
      this.head = temp.prev;
    }
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr) {
      result += curr.data + (curr.next ? ' ↔ ' : '');
      curr = curr.next;
    }
    console.log(result);
  }
}

// Driver code
const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
console.log('Original list:');
dll.printList();
dll.reverse();
console.log('Reversed list:');
dll.printList();
while (curr) {
iterate through each node to reverse links
temp = curr.prev; curr.prev = curr.next; curr.next = temp;
swap next and prev pointers to reverse direction
curr = curr.prev;
move to next node using old next pointer (now prev) after swap
if (temp) { this.head = temp.prev; }
update head to new first node after reversal
OutputSuccess
Original list: 1 ↔ 2 ↔ 3 Reversed list: 3 ↔ 2 ↔ 1
Complexity Analysis
Time: O(n) because we visit each node once to swap pointers
Space: O(1) because we only use a few extra variables for swapping
vs Alternative: Compared to creating a new reversed list (O(n) time and space), this reverses in place with no extra space
Edge Cases
empty list
reverse does nothing, head remains null
DSA Javascript
while (curr) {
single node list
reverse swaps pointers but list remains same, head unchanged
DSA Javascript
while (curr) {
When to Use This Pattern
When you need to reverse the order of a doubly linked list, look for swapping next and prev pointers of each node and updating the head pointer.
Common Mistakes
Mistake: Moving curr to curr.next after swapping pointers instead of curr.prev
Fix: After swapping, move curr to curr.prev because next and prev are swapped
Mistake: Not updating the head pointer after reversal
Fix: Update head to the last processed node using temp pointer after loop
Summary
Reverses a doubly linked list by swapping next and prev pointers of each node.
Use when you want to reverse the order of elements in a doubly linked list efficiently.
The key is to swap pointers and move forward using the old next pointer (now prev) after swapping.

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