Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after removing 2nd node from end
What is the output linked list after removing the 2nd node from the end in the given list?
DSA C
struct Node {
int val;
struct Node* next;
};
// List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
// Remove 2nd node from end
struct Node* removeNthFromEnd(struct Node* head, int n) {
struct Node* fast = head;
struct Node* slow = head;
for (int i = 0; i < n; i++) {
fast = fast->next;
}
if (!fast) {
struct Node* temp = head;
head = head->next;
free(temp);
return head;
}
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
struct Node* temp = slow->next;
slow->next = slow->next->next;
free(temp);
return head;
}Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the node before the last node.
✗ Incorrect
Removing the 2nd node from the end means removing the node with value 4. The resulting list is 1 -> 2 -> 3 -> 5 -> NULL.
❓ Predict Output
intermediate2:00remaining
Output after removing 1st node from end
What is the output linked list after removing the last node (1st from end) from the list?
DSA C
struct Node {
int val;
struct Node* next;
};
// List: 10 -> 20 -> 30 -> NULL
// Remove 1st node from end
struct Node* removeNthFromEnd(struct Node* head, int n) {
struct Node* fast = head;
struct Node* slow = head;
for (int i = 0; i < n; i++) {
fast = fast->next;
}
if (!fast) {
struct Node* temp = head;
head = head->next;
free(temp);
return head;
}
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
struct Node* temp = slow->next;
slow->next = slow->next->next;
free(temp);
return head;
}Attempts:
2 left
💡 Hint
Removing the last node means the node with value 30 is removed.
✗ Incorrect
The last node (value 30) is removed, so the list becomes 10 -> 20 -> NULL.
🔧 Debug
advanced2:00remaining
Identify the error in removing nth node from end
What error will this code produce when removing the 3rd node from end in a 2-node list?
DSA C
struct Node* removeNthFromEnd(struct Node* head, int n) { struct Node* fast = head; struct Node* slow = head; for (int i = 0; i < n; i++) { fast = fast->next; } if (!fast) { struct Node* temp = head; head = head->next; free(temp); return head; } while (fast->next) { fast = fast->next; slow = slow->next; } struct Node* temp = slow->next; slow->next = slow->next->next; free(temp); return head; } // List: 1 -> 2 -> NULL // Remove 3rd node from end
Attempts:
2 left
💡 Hint
Check what happens when fast pointer moves beyond list length.
✗ Incorrect
The fast pointer moves 3 steps but list has only 2 nodes, so fast becomes NULL and next access causes segmentation fault.
🧠 Conceptual
advanced2:00remaining
Why use two pointers in removing nth node from end?
Why does the two-pointer technique work for removing the nth node from the end of a linked list?
Attempts:
2 left
💡 Hint
Think about how the gap helps locate the node to remove.
✗ Incorrect
Moving one pointer n steps ahead creates a fixed gap so when the fast pointer reaches the end, the slow pointer is at the node before the target.
❓ Predict Output
expert3:00remaining
Output after removing 4th node from end in 5-node list
Given the list 5 -> 10 -> 15 -> 20 -> 25 -> NULL, what is the output after removing the 4th node from the end?
DSA C
struct Node* removeNthFromEnd(struct Node* head, int n) { struct Node* fast = head; struct Node* slow = head; for (int i = 0; i < n; i++) { fast = fast->next; } if (!fast) { struct Node* temp = head; head = head->next; free(temp); return head; } while (fast->next) { fast = fast->next; slow = slow->next; } struct Node* temp = slow->next; slow->next = slow->next->next; free(temp); return head; } // Remove 4th node from end
Attempts:
2 left
💡 Hint
Count nodes from end to find which node is removed.
✗ Incorrect
The 4th node from end is the node with value 10. Removing it results in 5 -> 15 -> 20 -> 25 -> NULL.
