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 Typescript
function reverseList(head: ListNode | null): ListNode | null {
if (head === null || head.next === null) {
return [1];
}
// recursive call
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning head.next instead of head in base case.
Returning null always without checking conditions.
✗ 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 Typescript
function reverseList(head: ListNode | null): ListNode | null {
if (head === null || head.next === null) {
return head;
}
const newHead = [1];
// rest of reversal
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling reverseList on head instead of head.next causing infinite recursion.
Assigning head.next directly without recursion.
✗ Incorrect
We recursively call reverseList on head.next to reverse the rest of the list.
3fill in blank
hardFix the error in setting the next pointer of the next node to point back to head.
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
if (head === null || head.next === null) {
return head;
}
const newHead = reverseList(head.next);
head.next.[1] = head;
head.next = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev which does not exist in singly linked list.
Using next.prev which is invalid.
✗ Incorrect
We set head.next.next = head to reverse the link direction.
4fill in blank
hardFill both blanks to complete the recursive reversal and break the old link.
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
if (head === null || head.next === null) {
return head;
}
const newHead = reverseList([1]);
[2] = head;
head.next = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using newHead.next instead of head.next.next.
Not breaking the old link by setting head.next = null.
✗ Incorrect
We recursively reverse from head.next, then set head.next.next to head to reverse the link.
5fill in blank
hardFill all three blanks to complete the recursive reverse function with proper return and link reversal.
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
if ([1] || [2]) {
return head;
}
const newHead = reverseList(head.next);
head.next.[3] = head;
head.next = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev which does not exist in singly linked list.
Incorrect base case conditions.
✗ Incorrect
The base case checks if head is null or head.next is null, then returns head. The reversal sets head.next.next = head.
