Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the Node class with a constructor that initializes the value and next properties.
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'this.next' instead of 'this.value'.
Assigning 'null' or 'undefined' to 'this.value'.
✗ Incorrect
The constructor parameter 'value' should be assigned to the instance property 'this.value'.
2fill in blank
mediumComplete the code to initialize the head property of the LinkedList class to null.
DSA Typescript
class LinkedList { head: Node | null; constructor() { this.head = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'undefined' instead of 'null'.
Initializing head with a new Node instead of null.
✗ Incorrect
The head of an empty linked list should be initialized to null.
3fill in blank
hardFix the error in the add method to correctly add a new node at the end of the list.
DSA Typescript
add(value: number): void {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== [1]) {
current = current.next;
}
current.next = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'undefined' instead of 'null'.
Comparing to '0' or 'newNode' incorrectly.
✗ Incorrect
The loop should continue until current.next is null, meaning the end of the list.
4fill in blank
hardFill both blanks to complete the printList method that prints all node values separated by arrows.
DSA Typescript
printList(): void {
let current = this.head;
let result = '';
while (current !== [1]) {
result += current.value + [2];
current = current.next;
}
console.log(result + 'null');
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'undefined' instead of 'null' in the loop condition.
Missing spaces around the arrow string.
✗ Incorrect
The loop runs while current is not null, and values are joined with ' -> ' arrows.
5fill in blank
hardFill all three blanks to complete the remove method that deletes the first node with the given value.
DSA Typescript
remove(value: number): void {
if (this.head === [1]) {
return;
}
if (this.head.value === value) {
this.head = this.head.next;
return;
}
let current = this.head;
while (current !== null && current.next !== [2]) {
if (current.next.value === value) {
current.next = current.next.[3];
return;
}
current = current.next;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'undefined' instead of 'null' for checks.
Assigning current.next to 'head' or 'undefined' incorrectly.
✗ Incorrect
Check if head is null (return early), then check if head.value === value (remove head if match), then traverse while current.next !== null, and update current.next to current.next.next to skip the removed node.
