Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'value' for assignment.
Assigning 'null' to 'value' property.
✗ Incorrect
The constructor assigns the input parameter 'value' to the node's 'value' property.
2fill in blank
mediumComplete the code to check if the list is empty before inserting at the end.
DSA Typescript
insertAtEnd(value: number): void {
const newNode = new Node(value);
if (this.head === [1]) {
this.head = newNode;
return;
}
// continue to insert at end
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head equals undefined instead of null.
Checking if head equals newNode.
✗ Incorrect
The list is empty if 'this.head' is null, so we check for null.
3fill in blank
hardFix the error in the loop that finds the last node before inserting.
DSA Typescript
let current = this.head; while (current.[1] !== null) { current = current.next; } current.next = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' instead of 'next' in the loop condition.
Using 'prev' which is not defined.
✗ Incorrect
We traverse the list by moving through 'next' pointers until the last node.
4fill in blank
hardFill both blanks to complete the tail insertion method.
DSA Typescript
insertAtEnd(value: number): void {
const newNode = new Node(value);
if (this.head === [1]) {
this.head = newNode;
return;
}
let current = this.head;
while (current.[2] !== null) {
current = current.next;
}
current.next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'undefined' instead of 'null' for empty check.
Using 'value' instead of 'next' in loop.
✗ Incorrect
Check if head is null to detect empty list, and traverse using 'next' pointers.
5fill in blank
hardFill all three blanks to create a method that inserts a node at the end and updates the tail pointer.
DSA Typescript
class LinkedList { head: Node | null = null; tail: Node | null = null; insertAtEnd(value: number): void { const newNode = new Node(value); if (this.head === [1]) { this.head = newNode; this.tail = newNode; return; } this.tail!.[2] = newNode; this.tail = [3]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating tail after insertion.
Using 'head' instead of 'newNode' when updating tail.
✗ Incorrect
If list is empty (head is null), set head and tail to newNode. Otherwise, link tail.next to newNode and update tail to newNode.
