Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node and insert it at the beginning of the linked list.
DSA Typescript
const newNode = { value: val, next: [1] };
this.head = newNode;Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to null breaks the chain.
Using tail instead of head causes incorrect linking.
✗ Incorrect
The new node's next should point to the current head to insert it at the beginning.
2fill in blank
mediumComplete the code to update the tail if the list was empty before insertion.
DSA Typescript
if (this.tail === null) { this.tail = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting tail to null loses the tail reference.
Using newNode.next sets tail to null incorrectly.
✗ Incorrect
If the list was empty, tail should point to the new node after insertion.
3fill in blank
hardFix the error in the insertAtBeginning method to correctly update the head pointer.
DSA Typescript
insertAtBeginning(val: number): void {
const newNode = { value: val, next: this.head };
this.head = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to val instead of newNode.
Assigning head to null breaks the list.
✗ Incorrect
The head must be updated to point to the new node after insertion.
4fill in blank
hardFill both blanks to complete the method that inserts a new node at the beginning and updates tail if needed.
DSA Typescript
insertAtBeginning(val: number): void {
const newNode = { value: val, next: [1] };
this.head = [2];
if (this.tail === null) {
this.tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of assignments.
Not updating tail when list is empty.
✗ Incorrect
The new node's next points to current head, then head updates to newNode.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning method with proper node creation, head update, and tail check.
DSA Typescript
insertAtBeginning(val: number): void {
const newNode = { value: val, next: [1] };
this.head = [2];
if (this.tail === [3]) {
this.tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using this.tail instead of null in the condition.
Not linking newNode correctly.
✗ Incorrect
newNode.next points to current head, head updates to newNode, tail updates if it was null.
