Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move the fast pointer n steps ahead.
DSA C++
for (int i = 0; i < [1]; i++) { fast = fast->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length of the list instead of n.
Moving the pointer less or more than n steps.
✗ Incorrect
We need to move the fast pointer exactly n steps ahead to find the nth node from the end.
2fill in blank
mediumComplete the code to move both pointers until fast reaches the end.
DSA C++
while (fast->next != nullptr) { slow = slow->next; fast = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using slow pointer instead of fast.
Using prev pointer which does not exist in singly linked list.
✗ Incorrect
We move the fast pointer to its next node to traverse the list.
3fill in blank
hardFix the error in removing the target node by completing the pointer update.
DSA C++
slow->next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting slow->next to nullptr which breaks the list.
Using fast pointer which is not at the correct position.
✗ Incorrect
To remove the nth node, link slow->next to the node after the target node.
4fill in blank
hardFill both blanks to handle the case when the head node is removed.
DSA C++
if (fast == nullptr) { head = [1]; return [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning old head instead of updated head.
Setting head to nullptr incorrectly.
✗ Incorrect
If fast is nullptr after moving n steps, the head is the node to remove. So, update head to head->next and return it.
5fill in blank
hardFill all three blanks to complete the function signature and return statement.
DSA C++
ListNode* [1](ListNode* [2], int [3]) { // function body return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function name.
Swapping parameter order.
✗ Incorrect
The function name is removeNthFromEnd, it takes head pointer and integer n as parameters.
