Bird
Raised Fist0
DSA C#programming~10 mins

Reverse a Singly Linked List Iterative in DSA C# - 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
Start from the head, reverse each node's pointer one by one until the end, then return the new head.
Execution Sample
DSA C#
Node ReverseList(Node head) {
  Node prev = null;
  Node current = head;
  while (current != null) {
    Node 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
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 current.next = prev (3)1 -> 2 -> 3 -> null1.next=null1 -> null, 2 -> 3 -> null
4Move prev = current (4)1 -> null, 2 -> 3 -> nullprev=11 -> null, 2 -> 3 -> null
5Move current = next (5)1 -> null, 2 -> 3 -> nullcurrent=21 -> null, 2 -> 3 -> null
6Save next = current.next (6)1 -> null, 2 -> 3 -> nullnext=31 -> null, 2 -> 3 -> null
7Reverse current.next = prev (7)1 -> null, 2 -> 3 -> null2.next=12 -> 1 -> null, 3 -> null
8Move prev = current (8)2 -> 1 -> null, 3 -> nullprev=22 -> 1 -> null, 3 -> null
9Move current = next (9)2 -> 1 -> null, 3 -> nullcurrent=32 -> 1 -> null, 3 -> null
10Save next = current.next (10)2 -> 1 -> null, 3 -> nullnext=null2 -> 1 -> null, 3 -> null
11Reverse current.next = prev (11)2 -> 1 -> null, 3 -> null3.next=23 -> 2 -> 1 -> null
12Move prev = current (12)3 -> 2 -> 1 -> nullprev=33 -> 2 -> 1 -> null
13Move current = next (13)3 -> 2 -> 1 -> nullcurrent=null3 -> 2 -> 1 -> null
14Loop ends, return prev as new head (14)3 -> 2 -> 1 -> nullreturn 33 -> 2 -> 1 -> null
💡 current becomes null at step 13, loop ends, prev points to new head
Variable Tracker
VariableStartAfter 1After 2After 3Final
prevnull1233
current123nullnull
nextundefined23nullnull
Key Moments - 3 Insights
Why do we set current.next = prev instead of prev.next = current?
Because we reverse the pointer of the current node to point backward. Setting prev.next = current would break the chain and not reverse the list. See execution_table rows 3, 7, and 11 where current.next is changed.
Why do we need to save next = current.next before reversing?
Because once we reverse current.next to prev, we lose the original next node. Saving next first lets us continue traversal. See execution_table rows 2, 6, and 10 where next is saved before reversing.
What happens when current becomes null?
It means we reached the end of the original list. The prev pointer now points to the new head of the reversed list. See execution_table row 13 where current=null ends the loop.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the new next pointer of node 2?
APoints to node 3
BPoints to node 1
CPoints to null
DPoints to node 2 itself
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 7 in execution_table.
At which step does the current pointer become null, ending the loop?
AStep 13
BStep 14
CStep 11
DStep 9
💡 Hint
Look at the 'current' variable in variable_tracker and the 'Pointer Changes' column in execution_table.
If we forget to save next = current.next before reversing, what happens?
Aprev becomes null
BThe list reverses correctly
CWe lose access to the rest of the list after current
Dcurrent becomes null immediately
💡 Hint
Refer to key_moments about why saving next is important and execution_table steps 2 and 3.
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 = prev
  * Move prev = current
  * Move current = next
- Return prev as new head
Pointers change direction step-by-step to reverse the list.
Full Transcript
This concept shows how to reverse a singly linked list using an iterative approach. We start with three pointers: prev set to null, current set to the head of the list, and next as a temporary pointer. In each step, we save the next node before reversing the current node's pointer to point backward to prev. Then we move prev and current forward by one node. This continues 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 and the list change. Key moments clarify why saving next is necessary and why we reverse current.next, not prev.next. The visual quiz tests understanding of pointer changes and loop termination.

Practice

(1/5)
1. What is the main purpose of the iterative method to reverse a singly linked list?
easy
A. To sort the nodes in ascending order
B. To delete all nodes from the list
C. To change the direction of the links between nodes one by one
D. To add new nodes at the end of the list

Solution

  1. Step 1: Understand the linked list structure

    A singly linked list has nodes connected in one direction through next pointers.
  2. Step 2: Purpose of iterative reversal

    The iterative method changes each node's next pointer to point backward, reversing the list.
  3. Final Answer:

    To change the direction of the links between nodes one by one -> Option C
  4. Quick Check:

    Reversing = changing links direction [OK]
Hint: Reversing means flipping the links one by one [OK]
Common Mistakes:
  • Thinking reversal deletes nodes
  • Confusing reversal with sorting
  • Assuming reversal adds nodes
2. Which of the following is the correct way to initialize pointers for iterative reversal of a singly linked list in C#?
easy
A. Node prev = head; Node current = null;
B. Node prev = null; Node current = null;
C. Node prev = head; Node current = head.next;
D. Node prev = null; Node current = head;

Solution

  1. Step 1: Initialization for reversal

    We start with prev as null because the new tail will point to null, and current at head to traverse.
  2. Step 2: Check each option

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

    Node prev = null; Node 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
  • Both pointers null at start
3. What will be the output linked list after running this code snippet on list 1 -> 2 -> 3 -> null?
Node prev = null;
Node current = head;
while (current != null) {
  Node nextTemp = current.next;
  current.next = prev;
  prev = current;
  current = nextTemp;
}
head = prev;
medium
A. 1 -> 2 -> 3 -> null
B. 3 -> 2 -> 1 -> null
C. null
D. 1 -> null

Solution

  1. Step 1: Trace the reversal loop

    Each iteration reverses the link: 1 points to null, 2 points to 1, 3 points to 2.
  2. Step 2: Final linked list after loop

    After loop, prev points to 3, which is new head: 3 -> 2 -> 1 -> null.
  3. Final Answer:

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

    Reversed list = 3->2->1 [OK]
Hint: Reversal flips list order completely [OK]
Common Mistakes:
  • Assuming list stays same
  • Forgetting to update head
  • Confusing next and prev pointers
4. Identify the error in this iterative reversal code snippet:
Node prev = head;
Node current = head.next;
while (current != null) {
  Node nextTemp = current.next;
  current.next = prev;
  prev = current;
  current = nextTemp;
}
head.next = null;
head = prev;
medium
A. prev should be initialized to null, not head
B. current should start at head, not head.next
C. head.next should not be set to null
D. No error, code is correct

Solution

  1. Step 1: Check pointer initialization

    prev must start as null to mark new tail's next as null; starting at head causes cycle.
  2. Step 2: Effect of wrong initialization

    Starting prev at head causes the first node to point to itself, creating a loop.
  3. Final Answer:

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

    Prev must be null at start [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
  • Starting current at wrong node
5. Given a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null, write the final linked list after applying iterative reversal twice in a row.
hard
A. 10 -> 20 -> 30 -> 40 -> null
B. 40 -> 30 -> 20 -> 10 -> null
C. null
D. 40 -> 20 -> 30 -> 10 -> null

Solution

  1. Step 1: First reversal

    Reversing 10->20->30->40 gives 40->30->20->10->null.
  2. Step 2: Second reversal

    Reversing again restores original order: 10->20->30->40->null.
  3. Final Answer:

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

    Double reversal restores original list [OK]
Hint: Reversing twice returns original list [OK]
Common Mistakes:
  • Assuming second reversal changes order again
  • Confusing node values after reversal
  • Forgetting reversal is reversible
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