Bird
Raised Fist0
DSA C#programming

Reverse a Singly Linked List Iterative in DSA C#

Choose your learning style10 modes available
Mental Model
We flip the direction of each arrow in the list one by one until the whole list points backward.
Analogy: Imagine a line of people holding hands forward. We ask each person to turn around and hold hands with the person behind them instead, reversing the line.
head -> 1 -> 2 -> 3 -> null
↑curr
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Reverse the list so it becomes 3 -> 2 -> 1 -> null
Step 1: Set prev to null, curr to head (node 1)
prev = null, curr = 1 -> 2 -> 3 -> null
Why: We start with prev as null because the new tail will point to null
Step 2: Save next node (2), reverse curr's pointer to prev (null), move prev to curr (1), move curr to next (2)
1 -> null [prev]
2 -> 3 -> null [curr]
Why: Reverse first link and move forward to continue reversing
Step 3: Save next node (3), reverse curr's pointer to prev (1), move prev to curr (2), move curr to next (3)
2 -> 1 -> null [prev]
3 -> null [curr]
Why: Reverse second link and move forward
Step 4: Save next node (null), reverse curr's pointer to prev (2), move prev to curr (3), move curr to next (null)
3 -> 2 -> 1 -> null [prev]
curr = null
Why: Reverse last link; curr is null means done
Result:
3 -> 2 -> 1 -> null
Annotated Code
DSA C#
using System;

public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int val=0, ListNode next=null) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next; // save next node
            curr.next = prev; // reverse pointer
            prev = curr; // move prev forward
            curr = nextTemp; // move curr forward
        }
        return prev; // new head
    }

    public void PrintList(ListNode head) {
        ListNode curr = head;
        while (curr != null) {
            Console.Write(curr.val);
            if (curr.next != null) Console.Write(" -> ");
            curr = curr.next;
        }
        Console.WriteLine(" -> null");
    }

    public static void Main() {
        ListNode node3 = new ListNode(3);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);

        Solution sol = new Solution();
        Console.WriteLine("Original list:");
        sol.PrintList(node1);

        ListNode reversed = sol.ReverseList(node1);
        Console.WriteLine("Reversed list:");
        sol.PrintList(reversed);
    }
}
ListNode prev = null; ListNode curr = head;
Initialize prev to null and curr to head to start reversal
while (curr != null) {
Loop until we reach the end of the list
ListNode nextTemp = curr.next; // save next node
Keep next node to continue traversal after reversal
curr.next = prev; // reverse pointer
Reverse current node's pointer to previous node
prev = curr; // move prev forward
Move prev pointer forward to current node
curr = nextTemp; // move curr forward
Advance curr to next node to continue reversal
return prev; // new head
Return prev as new head after full reversal
OutputSuccess
Original list: 1 -> 2 -> 3 -> null Reversed list: 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we visit each node once to reverse pointers
Space: O(1) because we only use a few pointers, no extra data structures
vs Alternative: Compared to recursive reversal which uses O(n) stack space, iterative is more space efficient
Edge Cases
empty list (head is null)
function returns null immediately, no reversal needed
DSA C#
while (curr != null) {
single node list
pointer reversal sets next to null but list remains same
DSA C#
curr.next = prev; // reverse pointer
When to Use This Pattern
When asked to reverse a singly linked list, use the iterative pointer reversal pattern to efficiently flip links in one pass.
Common Mistakes
Mistake: Not saving the next node before reversing pointer causes loss of access to rest of list
Fix: Always save curr.next in a temporary variable before changing curr.next
Mistake: Returning head instead of prev after reversal
Fix: Return prev as it points to new head after full reversal
Summary
Reverses the direction of a singly linked list by flipping pointers iteratively.
Use when you need to reverse a linked list efficiently without extra space.
The key is to save the next node before reversing the pointer to avoid losing the list.

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