Challenge - 5 Problems
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing a linked list with 3 nodes
What is the printed linked list after reversing it iteratively?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head = reverseList(head);
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Think about how the pointers change direction in each iteration.
✗ Incorrect
The iterative reversal changes the next pointer of each node to point to the previous node, so the list order reverses from 1->2->3 to 3->2->1.
❓ Predict Output
intermediateOutput when reversing an empty linked list
What is the output when the linked list is empty (head is nullptr) and reversed iteratively?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
int main() {
Node* head = nullptr;
head = reverseList(head);
if (head == nullptr) {
std::cout << "null" << std::endl;
} else {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Reversing an empty list should still be empty.
✗ Incorrect
If the input list is empty (nullptr), the reversal function returns nullptr, so printing results in 'null'.
🔧 Debug
advancedIdentify the bug in this iterative reversal code
What error will this code cause when reversing a linked list iteratively?
DSA C++
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextTemp = curr->next;
curr->next = curr; // Bug here
prev = curr;
curr = nextTemp;
}
return prev;
}Attempts:
2 left
💡 Hint
Check what happens when a node points to itself.
✗ Incorrect
Setting curr->next = curr creates a cycle where the node points to itself, causing an infinite loop during traversal.
❓ Predict Output
advancedOutput after reversing a 5-node linked list
What is the printed linked list after reversing this 5-node list iteratively?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr) {
Node* nextTemp = curr->next;
curr->next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
int main() {
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
head->next->next->next->next = new Node(50);
head = reverseList(head);
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The reversal flips the order of all nodes.
✗ Incorrect
The iterative reversal reverses the list from 10->20->30->40->50 to 50->40->30->20->10.
🧠 Conceptual
expertWhy does iterative reversal use three pointers?
In the iterative reversal of a singly linked list, why do we need to keep track of three pointers: prev, curr, and nextTemp?
Attempts:
2 left
💡 Hint
Think about what happens if you lose track of the next node.
✗ Incorrect
prev remembers the reversed part, curr is the current node, and nextTemp saves the next node before changing links, preventing loss of access.
