Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a node with a value and next pointer.
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to undefined or 0 instead of null.
✗ Incorrect
The next pointer should initially point to null because the node does not link to any other node yet.
2fill in blank
mediumComplete the code to initialize the head of the circular linked list pointing to itself.
DSA Javascript
class CircularLinkedList { constructor() { this.head = new Node(10); this.head.next = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to null breaks the circular structure.
✗ Incorrect
In a circular singly linked list with one node, the next pointer points back to the head node itself.
3fill in blank
hardFix the error in the insert method to correctly insert a new node after the head.
DSA Javascript
insert(value) {
const newNode = new Node(value);
newNode.next = this.head.next;
this.head.next = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head.next to head or value instead of newNode.
✗ Incorrect
The head's next pointer should point to the new node to insert it after the head.
4fill in blank
hardFill both blanks to complete the method that prints all values in the circular linked list.
DSA Javascript
printList() {
if (!this.head) return;
let current = this.head;
do {
console.log(current.value);
current = [1];
} while (current !== [2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using current instead of current.next causes infinite loop.
Comparing to current instead of head stops too early.
✗ Incorrect
We move current to current.next each time and stop when current reaches the head again.
5fill in blank
hardFill all three blanks to complete the method that searches for a value in the circular linked list.
DSA Javascript
search(value) {
if (!this.head) return false;
let current = this.head;
do {
if (current.[1] === [2]) return true;
current = [3];
} while (current !== this.head);
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'current.next' causes errors.
Comparing wrong properties or variables.
✗ Incorrect
We check if current.value equals the searched value and move to current.next each time.
