Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given value.
DSA C
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = [1];
newNode->next = NULL; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable name like 'val' or 'value'.
Confusing the node pointer with the value variable.
✗ Incorrect
The variable 'x' holds the value to assign to the new node's val field.
2fill in blank
mediumComplete the code to advance the fast pointer n steps ahead.
DSA C
for (int i = 0; i < [1]; i++) { fast = fast->next; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable like length or count which are unrelated here.
Off-by-one errors in the loop condition.
✗ Incorrect
We need to move the fast pointer n steps ahead, so the loop runs n times.
3fill in blank
hardFix the error in the condition to check if fast pointer reached the end.
DSA C
if (fast == [1]) { return head->next; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking against head or tail which are not correct here.
Using fast->next which can cause a null pointer dereference.
✗ Incorrect
When fast reaches NULL, it means we need to remove the head node.
4fill in blank
hardFill both blanks to move fast and slow pointers until fast reaches the end.
DSA C
while (fast->next != [1]) { fast = fast->next; slow = [2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using head or fast in place of slow->next.
Checking fast->next against head instead of NULL.
✗ Incorrect
We move fast until fast->next is NULL, and move slow one step forward each time.
5fill in blank
hardFill all three blanks to remove the nth node from end by adjusting pointers.
DSA C
struct ListNode* toDelete = slow->[1]; slow->[2] = toDelete->[3]; free(toDelete);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev pointers which do not exist in singly linked list.
Not freeing the deleted node causing memory leaks.
✗ Incorrect
We set toDelete as slow->next, then link slow->next to toDelete->next, and free toDelete.
