Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Reverse a Singly Linked List Iterative in DSA Typescript - 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
↓
Loop ends when current == null
↓
Return prev as new 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 becomes the new head.
Execution Sample
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    let next = current.next;
    current.next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
This code reverses the singly linked list by changing the next pointers iteratively.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
0Start1 -> 2 -> 3 -> nullprev = null, current = 11 -> 2 -> 3 -> null
1Save next1 -> 2 -> 3 -> nullnext = 21 -> 2 -> 3 -> null
2Reverse pointer1 -> 2 -> 3 -> null1.next = prev (null)1 -> null, 2 -> 3 -> null
3Move prev1 -> null, 2 -> 3 -> nullprev = 11 -> null, 2 -> 3 -> null
4Move current1 -> null, 2 -> 3 -> nullcurrent = 21 -> null, 2 -> 3 -> null
5Save next1 -> null, 2 -> 3 -> nullnext = 31 -> null, 2 -> 3 -> null
6Reverse pointer1 -> null, 2 -> 3 -> null2.next = prev (1)2 -> 1 -> null, 3 -> null
7Move prev2 -> 1 -> null, 3 -> nullprev = 22 -> 1 -> null, 3 -> null
8Move current2 -> 1 -> null, 3 -> nullcurrent = 32 -> 1 -> null, 3 -> null
9Save next2 -> 1 -> null, 3 -> nullnext = null2 -> 1 -> null, 3 -> null
10Reverse pointer2 -> 1 -> null, 3 -> null3.next = prev (2)3 -> 2 -> 1 -> null
11Move prev3 -> 2 -> 1 -> nullprev = 33 -> 2 -> 1 -> null
12Move current3 -> 2 -> 1 -> nullcurrent = null3 -> 2 -> 1 -> null
13End loop3 -> 2 -> 1 -> nullcurrent == null3 -> 2 -> 1 -> null
💡 Loop ends when current becomes null, prev points to new head (3)
Variable Tracker
VariableStartAfter Step 4After Step 8After Step 12Final
prevnull1233
current123nullnull
nextundefined23nullnull
List State1 -> 2 -> 3 -> null1 -> null, 2 -> 3 -> null2 -> 1 -> null, 3 -> null3 -> 2 -> 1 -> null3 -> 2 -> 1 -> null
Key Moments - 3 Insights
Why do we need to save 'next' before reversing the pointer?
Because once we reverse current.next to point to prev, we lose the original next node. Saving 'next' first (see steps 1 and 5) lets us continue traversing the list.
Why does the loop stop when current is null, not when prev is null?
The loop moves current forward each time. When current becomes null (step 12), it means we reached the end. Prev holds the new head of the reversed list.
What does the visual state show after reversing pointers?
It shows the list gradually reversing. For example, after step 6, node 2 points back to 1, breaking the original forward chain.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6, what does node 2 point to?
ANode 1
BNode 3
Cnull
DNode 2 itself
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 6.
At which step does the current pointer become null?
AStep 10
BStep 12
CStep 13
DStep 9
💡 Hint
Look at the 'Pointer Changes' column for current pointer updates.
If we forget to update prev = current, what happens to the reversed list?
AThe list reverses correctly anyway
BThe reversed list will be incomplete and incorrect
CThe loop will never end
DThe head will point to null immediately
💡 Hint
Refer to the 'Pointer Changes' column where prev is updated (steps 3,7,11).
Concept Snapshot
Reverse a singly linked list iteratively:
- Use three pointers: prev (null), current (head), next (temp)
- Loop while current != null:
  - Save next = current.next
  - Reverse current.next to prev
  - Move prev = current
  - Move current = next
- Return prev as new head
This reverses links step-by-step without extra space.
Full Transcript
To reverse a singly linked list iteratively, we start with two pointers: prev set to null and current set to the head of the list. We loop while current is not null. In each iteration, we save the next node, reverse the current node's next pointer to point to prev, then move prev and current forward by one node. When current becomes null, prev points to the new head of the reversed list. This method changes the direction of the pointers one by one, effectively reversing the list in place without extra memory.

Practice

(1/5)
1. What is the main purpose of reversing a singly linked list iteratively?
easy
A. To sort the linked list in ascending order
B. To change the direction of the links between nodes without using recursion
C. To delete all nodes from the linked list
D. To find the middle node of the linked list

Solution

  1. Step 1: Understand the operation of reversing

    Reversing a singly linked list means changing the direction of the next pointers so the last node becomes the first.
  2. Step 2: Identify iterative approach

    The iterative method changes links step-by-step using a loop without recursion.
  3. Final Answer:

    To change the direction of the links between nodes without using recursion -> Option B
  4. Quick Check:

    Reversing iteratively = changing links direction without recursion [OK]
Hint: Reversing means flipping links, not sorting or deleting [OK]
Common Mistakes:
  • Confusing reversing with sorting
  • Thinking recursion is required
  • Assuming nodes are deleted
2. Which of the following TypeScript code snippets correctly initializes pointers for iterative reversal of a singly linked list?
easy
A. let prev = null; let current = head;
B. let prev = head; let current = null;
C. let prev = head; let current = head.next;
D. let prev = null; let current = null;

Solution

  1. Step 1: Understand pointer roles

    Prev starts as null because the new tail points to null; current starts at head to traverse.
  2. Step 2: Match initialization

    Only let prev = null; let current = head; sets prev to null and current to head correctly for iteration.
  3. Final Answer:

    let prev = null; let current = head; -> Option A
  4. Quick Check:

    Prev null, current head = correct start [OK]
Hint: Prev starts null, current starts at head for iteration [OK]
Common Mistakes:
  • Setting prev to head initially
  • Starting current as null
  • Both pointers null at start
3. Given the linked list: 1 -> 2 -> 3 -> null, what will be the output after running this iterative reversal code?
function reverseList(head: ListNode | null): ListNode | null {
  let prev = null;
  let current = head;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}
medium
A. null
B. 1 -> 2 -> 3 -> null
C. 3 -> 2 -> 1 -> null
D. 1 -> null

Solution

  1. Step 1: Trace the reversal loop

    Initially prev=null, current=1. Each iteration reverses current.next to prev and moves pointers forward.
  2. Step 2: Final reversed list

    After loop, prev points to node 3, which links to 2, then 1, then null.
  3. Final Answer:

    3 -> 2 -> 1 -> null -> Option C
  4. Quick Check:

    Reversed list = 3 -> 2 -> 1 -> null [OK]
Hint: Follow pointers step-by-step to see reversed links [OK]
Common Mistakes:
  • Returning head instead of prev
  • Not updating current correctly
  • Confusing next pointer updates
4. Identify the error in this iterative reversal code snippet:
function reverseList(head: ListNode | null): ListNode | null {
  let prev = head;
  let current = head.next;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}
medium
A. current should start at head, not head.next
B. The function should return current instead of prev
C. nextTemp should be assigned after updating current.next
D. prev should be initialized to null, not head

Solution

  1. Step 1: Check pointer initialization

    Prev must start as null because the new tail points to null, not head.
  2. Step 2: Understand impact of wrong initialization

    Starting prev at head causes a cycle or incorrect reversal.
  3. Final Answer:

    prev should be initialized to null, not head -> Option D
  4. Quick Check:

    Prev must be null at start [OK]
Hint: Prev always null at start to avoid cycles [OK]
Common Mistakes:
  • Starting prev at head causing loops
  • Starting current at wrong node
  • Returning wrong pointer
5. You have a singly linked list with nodes containing values: 10 -> 20 -> 30 -> 40 -> null. After reversing it iteratively, you want to insert a new node with value 25 between 20 and 30 in the reversed list. What will be the correct order of nodes after insertion?
hard
A. 40 -> 30 -> 25 -> 20 -> 10 -> null
B. 40 -> 30 -> 20 -> 25 -> 10 -> null
C. 40 -> 30 -> 20 -> 10 -> 25 -> null
D. 40 -> 25 -> 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Reverse the original list

    Original: 10 -> 20 -> 30 -> 40 -> null reversed becomes 40 -> 30 -> 20 -> 10 -> null.
  2. Step 2: Insert 25 between 30 and 20

    In reversed list, 30 points to 20, so insert 25 after 30: 30 -> 25 -> 20.
  3. Step 3: Final list order

    40 -> 30 -> 25 -> 20 -> 10 -> null.
  4. Final Answer:

    40 -> 30 -> 25 -> 20 -> 10 -> null -> Option A
  5. Quick Check:

    Insert between 30 and 20 in reversed list = 40->30->25->20->10 [OK]
Hint: Reverse first, then insert at correct reversed position [OK]
Common Mistakes:
  • Inserting 25 between wrong nodes after reversal
  • Confusing original and reversed order
  • Not updating next pointers correctly
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