Bird
Raised Fist0
DSA C#programming~20 mins

Reverse a Singly Linked List Iterative in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Singly Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of reversing this linked list iteratively?
Given the linked list 1 -> 2 -> 3 -> null, what is the printed state after running the iterative reverse method?
DSA C#
public class Node {
    public int val;
    public Node next;
    public Node(int val) { this.val = val; this.next = null; }
}

public class LinkedList {
    public Node head;

    public void ReverseIterative() {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node nextTemp = current.next;
            current.next = prev;
            prev = current;
            current = nextTemp;
        }
        head = prev;
    }

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.val + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

var list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.ReverseIterative();
Console.WriteLine(list.PrintList());
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
C1 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
❓ Predict Output
intermediate
2:00remaining
What is the output after reversing an empty linked list?
If the linked list is empty (head is null), what will the PrintList method output after calling ReverseIterative?
DSA C#
public class Node {
    public int val;
    public Node next;
    public Node(int val) { this.val = val; this.next = null; }
}

public class LinkedList {
    public Node head;

    public void ReverseIterative() {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node nextTemp = current.next;
            current.next = prev;
            prev = current;
            current = nextTemp;
        }
        head = prev;
    }

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.val + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

var list = new LinkedList();
list.ReverseIterative();
Console.WriteLine(list.PrintList());
A0 -> null
Bnull
CException thrown
D -> null
Attempts:
2 left
💡 Hint
Consider what happens when the list is empty and you try to reverse it.
🔧 Debug
advanced
2:00remaining
Why does this iterative reverse code cause a runtime error?
Identify the cause of the runtime error in this iterative reverse method for a singly linked list.
DSA C#
public void ReverseIterative() {
    Node prev = null;
    Node current = head;
    while (current.next != null) {
        Node nextTemp = current.next;
        current.next = prev;
        prev = current;
        current = nextTemp;
    }
    head = prev;
}
AThe method does not update 'current' inside the loop, causing an infinite loop.
BThe variable 'prev' is not initialized properly, causing a null pointer exception.
CThe assignment 'head = prev;' should be inside the loop, causing incorrect reversal.
DThe loop condition should be 'current != null' instead of 'current.next != null', causing a null reference error.
Attempts:
2 left
💡 Hint
Check what happens when current is the last node and you access current.next.
❓ Predict Output
advanced
2:00remaining
What is the output after reversing a single-node linked list?
Given a linked list with a single node 5 -> null, what is the printed output after calling ReverseIterative?
DSA C#
public class Node {
    public int val;
    public Node next;
    public Node(int val) { this.val = val; this.next = null; }
}

public class LinkedList {
    public Node head;

    public void ReverseIterative() {
        Node prev = null;
        Node current = head;
        while (current != null) {
            Node nextTemp = current.next;
            current.next = prev;
            prev = current;
            current = nextTemp;
        }
        head = prev;
    }

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.val + " -> ";
            temp = temp.next;
        }
        return result + "null";
    }
}

var list = new LinkedList();
list.head = new Node(5);
list.ReverseIterative();
Console.WriteLine(list.PrintList());
A5 -> null
Bnull
C5 -> 5 -> null
DException thrown
Attempts:
2 left
💡 Hint
Think about what happens when there is only one node to reverse.
🧠 Conceptual
expert
2:00remaining
Why is iterative reversal of a singly linked list preferred over recursive reversal in some cases?
Select the best reason why iterative reversal of a singly linked list is often preferred over recursive reversal.
AIterative reversal is easier to implement and understand than recursive reversal.
BRecursive reversal is always slower than iterative reversal due to extra function calls.
CIterative reversal uses constant extra space, while recursive reversal uses stack space proportional to the list length.
DRecursive reversal cannot reverse lists with more than 100 nodes due to language limitations.
Attempts:
2 left
💡 Hint
Consider memory usage differences between recursion and iteration.

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