Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given value.
DSA Javascript
class Node { constructor(value) { this.value = [1]; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name like 'val' or 'data' instead of 'value'.
✗ Incorrect
The constructor parameter is named 'value', so we assign this.value = value.
2fill in blank
mediumComplete the code to insert a new node at the beginning of the linked list.
DSA Javascript
function insertAtHead(head, value) {
const newNode = new Node(value);
newNode.next = [1];
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null or value instead of head.
✗ Incorrect
The new node's next should point to the current head to insert at beginning.
3fill in blank
hardFix the error in the code to correctly insert at the beginning of the list.
DSA Javascript
function insertAtHead(head, value) {
const newNode = new Node(value);
newNode.next = head;
head = [1];
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to value or null instead of newNode.
✗ Incorrect
We must update head to point to the newNode after insertion.
4fill in blank
hardFill both blanks to complete the function that inserts a new node at the head and returns the updated list.
DSA Javascript
function insertAtHead(head, value) {
const newNode = new Node([1]);
newNode.next = [2];
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'newNode' or 'null' incorrectly in either blank.
✗ Incorrect
The new node is created with 'value' and its next points to current 'head'.
5fill in blank
hardFill all three blanks to implement a function that inserts a new node at the beginning and prints the list.
DSA Javascript
function insertAtHeadAndPrint(head, value) {
const newNode = new Node([1]);
newNode.next = [2];
head = newNode;
let current = head;
let result = '';
while (current !== null) {
result += current.value + ' -> ';
current = [3];
}
result += 'null';
console.log(result);
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for next pointer or traversal.
✗ Incorrect
Create newNode with 'value', link it to 'head', then traverse using 'current.next'.
