Challenge - 5 Problems
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reversing a linked list with 3 nodes
What is the printed output of the linked list after reversing it iteratively?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
head = reverseList(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
✗ Incorrect
The iterative reversal changes the next pointers of each node to point to the previous node, reversing the list order from 1->2->3 to 3->2->1.
❓ Predict Output
intermediate2:00remaining
Output after reversing a single-node linked list
What is the output when reversing a linked list with only one node?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 42;
head->next = NULL;
head = reverseList(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node points to NULL, so reversing it keeps the same node as head.
❓ Predict Output
advanced2:00remaining
Output of reversing a linked list with a cycle
What happens when you run the iterative reverse function on a linked list that contains a cycle?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* reverseList(Node* head) {
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = second; // cycle here
head = reverseList(head);
printf("Reversed list printed\n");
return 0;
}Attempts:
2 left
💡 Hint
Think about what happens when the list has a cycle and the loop condition depends on current != NULL.
✗ Incorrect
The reversal loop never ends because current never becomes NULL due to the cycle, causing an infinite loop.
❓ Predict Output
advanced2:00remaining
Final state of pointers after one iteration of reversal
Given the initial linked list 1 -> 2 -> 3 -> NULL, what are the values of prev, current, and next pointers after the first iteration of the reversal loop?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* head = malloc(sizeof(Node));
head->data = 1;
head->next = malloc(sizeof(Node));
head->next->data = 2;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 3;
head->next->next->next = NULL;
Node* prev = NULL;
Node* current = head;
Node* next = NULL;
// First iteration of reversal loop
next = current->next;
current->next = prev;
prev = current;
current = next;
printf("prev: %d, current: %d, next: %d\n", prev->data, current->data, next->data);
return 0;
}Attempts:
2 left
💡 Hint
Trace the pointer changes step by step for the first iteration.
✗ Incorrect
After first iteration, prev points to node with data 1, current and next point to node with data 2.
🧠 Conceptual
expert2:00remaining
Why iterative reversal is preferred over recursive for large lists
Which is the main reason iterative reversal of a singly linked list is preferred over recursive reversal for very large lists?
Attempts:
2 left
💡 Hint
Think about how recursion uses memory for each function call.
✗ Incorrect
Recursive reversal uses function call stack for each node, which can overflow for large lists. Iterative reversal uses constant extra space.
