Bird
Raised Fist0
DSA Javascriptprogramming~30 mins

Reverse a Doubly Linked List in DSA Javascript - Build from Scratch

Choose your learning style10 modes available
Reverse a Doubly Linked List
📖 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 to listen from the last to the first.
🎯 Goal: Build a doubly linked list with given songs, then reverse the list by swapping the links, and finally print the reversed list.
📋 What You'll Learn
Create a doubly linked list with nodes containing song names
Add a helper variable to track the current node during reversal
Reverse the doubly linked list by swapping the next and prev pointers
Print the reversed list showing song names in order
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history navigation), and many applications where you need to move forward and backward through items.
💼 Career
Understanding linked lists and how to reverse them is a common interview question and helps build strong problem-solving skills for software development roles.
Progress0 / 4 steps
1
Create the Doubly Linked List
Create a class called Node with properties song, prev, and next. Then create three nodes with songs 'Song A', 'Song B', and 'Song C'. Link them to form a doubly linked list: Song A <-> Song B <-> Song C. Store the head node in a variable called head.
DSA Javascript
Hint

Start by defining the Node class with the three properties. Then create three nodes and connect their next and prev pointers to link them in order.

2
Add a Current Node Variable
Create a variable called current and set it equal to head. This will help us move through the list during reversal.
DSA Javascript
Hint

Just create a variable current and assign it the value of head.

3
Reverse the Doubly Linked List
Use a while loop with the condition current !== null. Inside the loop, swap current.next and current.prev. Then move current to current.prev (which is the original next node). After the loop, update head to the last non-null current node by using a variable temp to track the previous node.
DSA Javascript
Hint

Inside the loop, swap the next and prev pointers of the current node. Move current to the original next node by assigning it to current.prev after swapping. After the loop, update head to the last node processed.

4
Print the Reversed List
Create a variable node and set it to head. Use a while loop to traverse the list until node is null. Inside the loop, print the song property of node. Move node to node.next. The output should show the songs in reversed order separated by arrows like: Song C -> Song B -> Song A.
DSA Javascript
Hint

Traverse from head using node.next. Build a string with song names separated by ->. Print the final string.

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