Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Reverse a Singly Linked List Iterative in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Reverse a Singly Linked List Iterative
Start with head node
↓
Initialize prev = null, current = head
↓
While current != null
↓
Save next = current.next
↓
Reverse pointer: current.next = prev
↓
Move prev = current
↓
Move current = next
↓
Repeat loop
↓
When current == null, prev is new head
↓
Return prev as reversed list head
We start from the head and move through the list, reversing each pointer one by one until we reach the end. The prev pointer tracks the new reversed list head.
Execution Sample
DSA Javascript
function reverseList(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    let next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
This code reverses a singly linked list by changing the next pointers iteratively.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initialize prev=null, current=head (1)1 -> 2 -> 3 -> nullprev=null, current=11 -> 2 -> 3 -> null
2Save next = current.next (2)1 -> 2 -> 3 -> nullnext=21 -> 2 -> 3 -> null
3Reverse pointer: current.next = prev (null)1 -> 2 -> 3 -> null1.next=null1 -> null, 2 -> 3 -> null
4Move prev = current (1)1 -> null, 2 -> 3 -> nullprev=11 -> null, 2 -> 3 -> null
5Move current = next (2)1 -> null, 2 -> 3 -> nullcurrent=21 -> null, 2 -> 3 -> null
6Save next = current.next (3)1 -> null, 2 -> 3 -> nullnext=31 -> null, 2 -> 3 -> null
7Reverse pointer: current.next = prev (1)1 -> null, 2 -> 3 -> null2.next=12 -> 1 -> null, 3 -> null
8Move prev = current (2)2 -> 1 -> null, 3 -> nullprev=22 -> 1 -> null, 3 -> null
9Move current = next (3)2 -> 1 -> null, 3 -> nullcurrent=32 -> 1 -> null, 3 -> null
10Save next = current.next (null)2 -> 1 -> null, 3 -> nullnext=null2 -> 1 -> null, 3 -> null
11Reverse pointer: current.next = prev (2)2 -> 1 -> null, 3 -> null3.next=23 -> 2 -> 1 -> null
12Move prev = current (3)3 -> 2 -> 1 -> nullprev=33 -> 2 -> 1 -> null
13Move current = next (null)3 -> 2 -> 1 -> nullcurrent=null3 -> 2 -> 1 -> null
14Loop ends, return prev as new head3 -> 2 -> 1 -> nullreturn prev3 -> 2 -> 1 -> null
💡 current becomes null at step 13, loop ends, prev points to new head
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 9After Step 13Final
prevnullnull1233
current1123nullnull
nextundefined223nullnull
Key Moments - 3 Insights
Why do we set current.next = prev instead of prev.next = current?
Because we want to reverse the direction of the current node's pointer to point backward. Setting prev.next = current would not change the current node's pointer and would break the list. See steps 3, 7, and 11 in the execution_table.
Why do we need to save next = current.next before reversing the pointer?
Because once we reverse current.next to prev, we lose the original next node reference. Saving next first lets us continue traversing the list. See steps 2, 6, and 10 in the execution_table.
What happens when current becomes null?
It means we reached the end of the original list. At this point, prev points to the new head of the reversed list. The loop ends and we return prev. See step 13 and exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what does current.next point to after reversal?
ANode 3
Bnull
CNode 1
DNode 2
💡 Hint
Check the 'Pointer Changes' column at step 7 where it says '2.next=1'
At which step does the current pointer become null, ending the loop?
AStep 12
BStep 13
CStep 14
DStep 11
💡 Hint
Look at the 'Pointer Changes' and 'Operation' columns where current=null
If we forget to save next = current.next before reversing, what happens?
AWe lose access to the rest of the list after current
Bprev becomes null
CThe list reverses correctly
Dcurrent becomes null immediately
💡 Hint
Refer to key_moments about why saving next is important (steps 2, 6, 10)
Concept Snapshot
Reverse a singly linked list iteratively:
- Use three pointers: prev (null), current (head), next
- Loop while current != null:
  * Save next = current.next
  * Reverse current.next to prev
  * Move prev = current
  * Move current = next
- Return prev as new head
Pointers reverse one by one, flipping list direction.
Full Transcript
This visualization shows how to reverse a singly linked list using an iterative method. We start with two pointers: prev set to null and current set to the head of the list. In each step, we save the next node to keep track of the remaining list. Then, we reverse the current node's pointer to point to prev. After that, we move prev to current and current to next. We repeat this until current becomes null, meaning we reached the end. At that point, prev points to the new head of the reversed list. The execution table tracks each step, showing how pointers change and how the list nodes connect differently after each reversal. Key moments explain why saving next is necessary and why we reverse current.next, not prev.next. The quiz tests understanding of pointer changes and loop termination. This method efficiently reverses the list in place without extra memory.

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

  1. Step 1: Understand the linked list order

    The original list goes from 1 to 3 in order: 1 -> 2 -> 3 -> null.
  2. Step 2: Reverse the pointers iteratively

    Reversing means the last node becomes first, so the list becomes 3 -> 2 -> 1 -> null.
  3. Final Answer:

    head -> 3 -> 2 -> 1 -> null -> Option A
  4. 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

  1. Step 1: Initialize previous pointer to null

    Before reversal, previous node is null because head has no previous.
  2. Step 2: Initialize current pointer to head

    Current starts at head to traverse the list.
  3. Final Answer:

    let prev = null, current = head; -> Option D
  4. 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

  1. Step 1: Trace reversal loop

    Each iteration reverses the next pointer: 1->2->3 becomes 3->2->1.
  2. Step 2: Collect values from reversed list

    After reversal, traversing from prev (new head) gives values 3, 2, 1.
  3. Final Answer:

    [3, 2, 1] -> Option B
  4. 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

  1. Step 1: Check initial pointer setup

    prev must start as null because head has no previous node before reversal.
  2. Step 2: Understand impact of wrong prev initialization

    Starting prev as head causes a cycle or incorrect links during reversal.
  3. Final Answer:

    prev should start as null, not head -> Option C
  4. 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

  1. Step 1: Reverse first 3 nodes iteratively

    Reverse nodes 1, 2, 3 to get 3 -> 2 -> 1.
  2. Step 2: Connect reversed part to remaining list

    Node 1 (now last in reversed part) points to node 4, preserving rest of list.
  3. Final Answer:

    3 -> 2 -> 1 -> 4 -> 5 -> null -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard