Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case node in the recursive reverse function.
DSA Javascript
if (head === null || head.next === null) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning head.next causes errors when head.next is null.
Returning null loses the last node reference.
✗ Incorrect
The base case returns the current node 'head' when it is the last node or the list is empty.
2fill in blank
mediumComplete the code to recursively reverse the rest of the list after the head.
DSA Javascript
const newHead = [1](head.next);Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined function name causes runtime errors.
Calling the function with wrong arguments.
✗ Incorrect
The function calls itself recursively to reverse the list starting from head.next.
3fill in blank
hardFix the error in the code to correctly reverse the link between head and head.next.
DSA Javascript
head.next.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.next.next to head.next causes a cycle.
Setting it to null breaks the chain.
✗ Incorrect
We set head.next.next to head to reverse the link direction.
4fill in blank
hardFill both blanks to break the old link and return the new head.
DSA Javascript
head.next = [1]; return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Not breaking the old link causes cycles.
Returning head instead of newHead returns the wrong node.
✗ Incorrect
Set head.next to null to break old link, then return newHead as the new list head.
5fill in blank
hardFill all three blanks to complete the recursive reverse function.
DSA Javascript
function reverseList(head) {
if (head === null || head.next === null) {
return [1];
}
const newHead = reverseList(head.next);
head.next.next = [2];
head.next = [3];
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning null in base case loses nodes.
Not breaking old link causes cycles.
Incorrectly setting pointers causes runtime errors.
✗ Incorrect
Return head in base case, set head.next.next to head to reverse link, then break old link by setting head.next to null.
