Challenge - 5 Problems
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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 ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* fast = head;
ListNode* slow = head;
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
if (!fast) {
ListNode* newHead = head->next;
delete head;
return newHead;
}
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
ListNode* toDelete = slow->next;
slow->next = slow->next->next;
delete toDelete;
return head;
}
// Initial list: 1 -> 2 -> 3 -> 4 -> 5 -> null
// Remove 2nd node from end (node with value 4)
// Print resulting listAttempts:
2 left
💡 Hint
Use two pointers with a gap of n nodes to find the node before the one to remove.
✗ Incorrect
The function moves the fast pointer n steps ahead, then moves both pointers until fast reaches the end. The slow pointer then points to the node before the one to remove. Removing the 2nd node from the end removes the node with value 4.
❓ Predict Output
intermediateOutput after removing 1st node from end
What is the output linked list after removing the last node (1st from end) in the given list?
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* fast = head;
ListNode* slow = head;
for (int i = 0; i < n; ++i) {
fast = fast->next;
}
if (!fast) {
ListNode* newHead = head->next;
delete head;
return newHead;
}
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
ListNode* toDelete = slow->next;
slow->next = slow->next->next;
delete toDelete;
return head;
}
// Initial list: 1 -> 2 -> 3 -> 4 -> 5 -> null
// Remove 1st node from end (node with value 5)
// Print resulting listAttempts:
2 left
💡 Hint
Removing the last node means the second last node's next becomes null.
✗ Incorrect
The last node (value 5) is removed, so the list ends at node with value 4.
🔧 Debug
advancedIdentify the error in this Remove Nth Node code
What error will this code produce when removing the 3rd node from end in a 3-node list?
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* fast = head;
ListNode* slow = head;
for (int i = 0; i <= n; ++i) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
ListNode* toDelete = slow->next;
slow->next = slow->next->next;
delete toDelete;
return head;
}
// List: 1 -> 2 -> 3 -> null
// Remove 3rd node from end (node with value 1)Attempts:
2 left
💡 Hint
Check the for loop condition and how many times fast moves.
✗ Incorrect
The for loop moves fast pointer n+1 times, which causes fast to become null or invalid when n equals list length, leading to null pointer dereference in the while loop.
🧠 Conceptual
advancedWhy use two pointers in Remove 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 just before the node to remove, enabling removal in one pass.
❓ Predict Output
expertOutput after removing 5th node from end in single-node list
What happens when you try to remove the 5th node from the end in a list with only one node?
DSA C++
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* fast = head;
ListNode* slow = head;
for (int i = 0; i < n; ++i) {
if (!fast) return head; // n is larger than list length
fast = fast->next;
}
if (!fast) {
ListNode* newHead = head->next;
delete head;
return newHead;
}
while (fast->next) {
fast = fast->next;
slow = slow->next;
}
ListNode* toDelete = slow->next;
slow->next = slow->next->next;
delete toDelete;
return head;
}
// List: 1 -> null
// Remove 5th node from endAttempts:
2 left
💡 Hint
Check how the code handles n larger than list length.
✗ Incorrect
The code checks if fast becomes null before moving n steps, returning the original list unchanged if n is too large.
