Challenge - 5 Problems
Singly Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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());
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
✗ Incorrect
The iterative reverse method changes the next pointers of each node to point to the previous node, effectively reversing the list. So the original 1 -> 2 -> 3 -> null becomes 3 -> 2 -> 1 -> null.
❓ Predict Output
intermediateWhat 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());
Attempts:
2 left
💡 Hint
Consider what happens when the list is empty and you try to reverse it.
✗ Incorrect
If the list is empty, head is null. The reverse method does nothing and head remains null. Printing the list results in just 'null'.
🔧 Debug
advancedWhy 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;
}Attempts:
2 left
💡 Hint
Check what happens when current is the last node and you access current.next.
✗ Incorrect
The loop condition 'current.next != null' causes the loop to stop before processing the last node. When current becomes the last node, current.next is null, so accessing current.next in the condition is safe, but the last node is never reversed. Also, after the loop, head is set to prev which points to the second last node, losing the last node. The correct condition is 'current != null' to process all nodes.
❓ Predict Output
advancedWhat 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());
Attempts:
2 left
💡 Hint
Think about what happens when there is only one node to reverse.
✗ Incorrect
Reversing a single-node list results in the same list because there is only one node. The output remains '5 -> null'.
🧠 Conceptual
expertWhy 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.
Attempts:
2 left
💡 Hint
Consider memory usage differences between recursion and iteration.
✗ Incorrect
Recursive reversal uses the call stack for each node, which can lead to stack overflow for large lists. Iterative reversal uses a few pointers and constant extra space, making it more memory efficient.
