Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a node class for the circular singly linked list.
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to this causes the node to point to itself immediately.
Using undefined instead of null can cause type errors.
✗ Incorrect
The next pointer should start as null because the node does not point to any other node initially.
2fill in blank
mediumComplete the code to initialize the circular singly linked list with a single node.
DSA Typescript
class CircularSinglyLinkedList { head: Node | null; constructor(value: number) { const newNode = new Node(value); newNode.next = [1]; this.head = newNode; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to null breaks the circular property.
Using this.head before assignment causes undefined errors.
✗ Incorrect
In a circular singly linked list with one node, the node's next points to itself.
3fill in blank
hardFix the error in the insert method to add a new node after the head.
DSA Typescript
insert(value: number) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
newNode.next = newNode;
} else {
newNode.next = this.head.[1];
this.head.next = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev causes undefined errors because prev is not defined.
Using head or value is incorrect for next pointer.
✗ Incorrect
To insert after head, newNode.next should point to head.next.
4fill in blank
hardFill both blanks to complete the method that prints all values in the circular singly linked list.
DSA Typescript
printList() {
if (!this.head) return;
let current = this.head;
do {
console.log(current.value);
current = current.[1];
} while (current !== this.[2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev causes errors because it's not defined.
Comparing to value instead of head breaks the loop condition.
✗ Incorrect
Traverse using current.next and stop when current loops back to head.
5fill in blank
hardFill all three blanks to complete the method that deletes the node after the head in the circular singly linked list.
DSA Typescript
deleteAfterHead() {
if (!this.head || this.head.next === this.head) return;
const toDelete = this.head.[1];
this.head.[2] = toDelete.[3];
toDelete.next = null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using prev causes errors because it's not defined.
Not updating head.next breaks the list.
✗ Incorrect
Delete the node after head by linking head.next to the node after the deleted node.
