Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Javascript
function Node(value) {
this.data = value;
this.next = [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to undefined instead of null.
Setting next to 0 or false which are not valid pointers.
✗ Incorrect
When creating a new node, its next pointer should be null because it does not point to any node yet.
2fill in blank
mediumComplete the code to check if the linked list is empty before inserting.
DSA Javascript
function insertAtEnd(head, value) {
let newNode = new Node(value);
if (head [1] null) {
return newNode;
}
// rest of code omitted
}Attempts:
3 left
💡 Hint
Common Mistakes
Using !== which checks for not equal.
Using > or < which are not suitable for null checks.
✗ Incorrect
To check if the list is empty, we compare head to null using strict equality ===.
3fill in blank
hardFix the error in the loop that finds the last node.
DSA Javascript
let current = head; while (current.next [1] null) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using === which stops immediately.
Using == or != which are less strict and can cause bugs.
✗ Incorrect
The loop should continue while current.next is not null, so use !== for strict inequality.
4fill in blank
hardFill both blanks to correctly insert the new node at the end.
DSA Javascript
let current = head; while (current.next [1] null) { current = current.next; } current.next = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using === in the loop condition which stops too early.
Setting current.next to null instead of the new node.
✗ Incorrect
The loop continues while current.next is not null, then sets current.next to the new node to insert at the end.
5fill in blank
hardFill all three blanks to complete the insertAtEnd function.
DSA Javascript
function insertAtEnd(head, value) {
let newNode = new Node([1]);
if (head [2] null) {
return newNode;
}
let current = head;
while (current.next [3] null) {
current = current.next;
}
current.next = newNode;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or null instead of value when creating new node.
Using != or !== incorrectly in conditions.
Not returning head after insertion.
✗ Incorrect
We create a new node with the given value, check if head is null using ===, and loop while current.next !== null to find the last node.
