📖 Scenario: Imagine you have a chain of boxes connected one after another. You want to reverse the order of these boxes so the last one becomes the first and the first becomes the last.
🎯 Goal: You will build a simple singly linked list with three nodes and then reverse it using an iterative method. Finally, you will print the reversed list to see the new order.
📋 What You'll Learn
Create a singly linked list with three nodes having values 1, 2, and 3
Use variables named head, node1, node2, and node3
Create a variable called prev and set it to null
Use a while loop to reverse the links iteratively
Print the reversed linked list values separated by ' -> ' and ending with ' -> null'
💡 Why This Matters
🌍 Real World
Reversing linked lists is useful in many applications like undo features, navigation history, and data processing pipelines where order needs to be changed.
💼 Career
Understanding linked list reversal is a common interview question and helps build strong problem-solving skills for software development roles.
Progress0 / 4 steps
1
Create the initial singly linked list
Create three nodes named node1, node2, and node3 as objects with value and next properties. Set their values to 1, 2, and 3 respectively. Link them so that node1.next is node2, node2.next is node3, and node3.next is null. Finally, create a variable called head and set it to node1.
DSA Javascript
Hint
Think of each node as a box with a value and a pointer to the next box.
2
Set up the previous pointer for reversal
Create a variable called prev and set it to null. This will help keep track of the previous node while reversing the list.
DSA Javascript
Hint
This variable will hold the previous node as you move through the list.
3
Reverse the linked list iteratively
Use a while loop with a variable called current starting at head. Inside the loop, save current.next in a variable called nextNode. Then set current.next to prev. Move prev to current and current to nextNode. After the loop ends, set head to prev.
DSA Javascript
Hint
Think of changing the direction of each arrow one by one until the whole chain is reversed.
4
Print the reversed linked list
Create a variable called temp and set it to head. Use a while loop to go through the list. In each loop, print temp.value followed by ' -> '. Move temp to temp.next. After the loop, print 'null' to show the end of the list.
DSA Javascript
Hint
Print each node's value followed by an arrow until you reach the end.
Practice
(1/5)
1. What does reversing a singly linked list iteratively mean? head -> 1 -> 2 -> 3 -> null becomes?
easy
A. head -> 3 -> 2 -> 1 -> null
B. head -> 1 -> 2 -> 3 -> null
C. head -> null -> 1 -> 2 -> 3
D. head -> 2 -> 3 -> 1 -> null
Solution
Step 1: Understand the linked list order
The original list goes from 1 to 3 in order: 1 -> 2 -> 3 -> null.
Step 2: Reverse the pointers iteratively
Reversing means the last node becomes first, so the list becomes 3 -> 2 -> 1 -> null.
Final Answer:
head -> 3 -> 2 -> 1 -> null -> Option A
Quick Check:
Reverse order = head -> 3 -> 2 -> 1 -> null [OK]
Hint: Reversing flips the list order completely [OK]
Common Mistakes:
Thinking the list stays the same after reverse
Confusing node values with pointers
Assuming null moves to front
2. Which of the following is the correct way to initialize pointers for iterative reversal of a singly linked list in JavaScript?
easy
A. let prev = null, current = null;
B. let prev = head, current = null;
C. let prev = head, current = head.next;
D. let prev = null, current = head;
Solution
Step 1: Initialize previous pointer to null
Before reversal, previous node is null because head has no previous.
Step 2: Initialize current pointer to head
Current starts at head to traverse the list.
Final Answer:
let prev = null, current = head; -> Option D
Quick Check:
Start prev=null, current=head [OK]
Hint: Prev starts null, current starts at head [OK]
Common Mistakes:
Setting prev to head initially
Setting current to null at start
Swapping initialization order
3. What is the output of the following code?
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}
// Create list: 1 -> 2 -> 3 -> null
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let reversedHead = reverseList(head);
let result = [];
let node = reversedHead;
while (node) {
result.push(node.val);
node = node.next;
}
console.log(result);
medium
A. []
B. [3, 2, 1]
C. [null]
D. [1, 2, 3]
Solution
Step 1: Trace reversal loop
Each iteration reverses the next pointer: 1->2->3 becomes 3->2->1.
Step 2: Collect values from reversed list
After reversal, traversing from prev (new head) gives values 3, 2, 1.
Final Answer:
[3, 2, 1] -> Option B
Quick Check:
Reversed list values = [3, 2, 1] [OK]
Hint: Reversed list values appear in reverse order [OK]
Common Mistakes:
Returning original head instead of prev
Not updating current pointer correctly
Collecting values before reversal
4. Find the error in this iterative reversal code snippet:
function reverseList(head) {
let prev = head;
let current = head.next;
while (current !== null) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
head.next = null;
return prev;
}
medium
A. head.next should not be set to null
B. current should start as head, not head.next
C. prev should start as null, not head
D. nextTemp should be assigned after changing current.next
Solution
Step 1: Check initial pointer setup
prev must start as null because head has no previous node before reversal.
Step 2: Understand impact of wrong prev initialization
Starting prev as head causes a cycle or incorrect links during reversal.
Final Answer:
prev should start as null, not head -> Option C
Quick Check:
prev=null start fixes reversal [OK]
Hint: Prev must start null to avoid cycles [OK]
Common Mistakes:
Starting prev at head causing loops
Not setting head.next to null after reversal
Changing nextTemp after modifying current.next
5. Given a singly linked list with nodes containing values 1 -> 2 -> 3 -> 4 -> 5 -> null, write an iterative function to reverse only the first 3 nodes. What will be the list after reversal?
hard
A. 3 -> 2 -> 1 -> 4 -> 5 -> null
B. 3 -> 2 -> 1 -> null
C. 5 -> 4 -> 3 -> 2 -> 1 -> null
D. 1 -> 2 -> 3 -> 4 -> 5 -> null
Solution
Step 1: Reverse first 3 nodes iteratively
Reverse nodes 1, 2, 3 to get 3 -> 2 -> 1.
Step 2: Connect reversed part to remaining list
Node 1 (now last in reversed part) points to node 4, preserving rest of list.
Final Answer:
3 -> 2 -> 1 -> 4 -> 5 -> null -> Option A
Quick Check:
Partial reversal keeps rest intact [OK]
Hint: Reverse only first k nodes, link rest after [OK]
Common Mistakes:
Reversing entire list instead of first 3 nodes
Not reconnecting reversed part to rest
Breaking list by losing tail connections
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.