Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the first node from the linked list.
DSA Typescript
function pop(head: ListNode | null): ListNode | null {
if (head === null) return null;
return head[1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning head.prev instead of head.next.
Returning head.value instead of the next node.
✗ Incorrect
To pop the first node, we return the next node after head, which is head.next.
2fill in blank
mediumComplete the code to update the head pointer after popping the first node.
DSA Typescript
function pop(head: ListNode | null): ListNode | null {
if (head === null) return null;
const newHead = head[1];
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.prev instead of head.next.
Returning the old head instead of the new head.
✗ Incorrect
After popping, the new head is the node after the current head, which is head.next.
3fill in blank
hardFix the error in the pop function to correctly remove the first node and avoid memory leaks.
DSA Typescript
function pop(head: ListNode | null): ListNode | null {
if (head === null) return null;
const newHead = head.next;
head[1] = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head.prev to null instead of head.next.
Not disconnecting the popped node, causing memory leaks.
✗ Incorrect
Setting head.next to null disconnects the popped node from the list, helping garbage collection.
4fill in blank
hardFill both blanks to correctly pop the first node and update the new head's previous pointer.
DSA Typescript
function pop(head: ListNode | null): ListNode | null {
if (head === null) return null;
const newHead = head[1];
if (newHead !== null) {
newHead[2] = null;
}
head.next = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newHead.next to null instead of newHead.prev.
Not updating newHead.prev, causing incorrect list links.
✗ Incorrect
After popping, newHead is head.next, and its prev pointer should be set to null to mark it as the new head.
5fill in blank
hardFill all three blanks to pop the first node, update the new head's previous pointer, and return the new head.
DSA Typescript
function pop(head: ListNode | null): ListNode | null {
if (head === null) return null;
const newHead = head[1];
if (newHead !== null) {
newHead[2] = null;
}
head[3] = null;
return newHead;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.prev or head.value instead of head.next.
Not disconnecting the old head's next pointer.
✗ Incorrect
We get newHead as head.next, set newHead.prev to null, and disconnect old head's next pointer by setting head.next to null.
