Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given data.
DSA Javascript
class Node { constructor(data) { this.data = [1]; this.prev = null; this.next = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning to this.value instead of this.data.
✗ Incorrect
The constructor should assign the passed data to this.data to store the node's value.
2fill in blank
mediumComplete the code to set the new node's next pointer to the current head.
DSA Javascript
function insertAtBeginning(head, data) {
const newNode = new Node(data);
newNode.next = [1];
if (head !== null) {
head.prev = newNode;
}
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to null or data instead of head.
Setting newNode.next to newNode itself.
✗ Incorrect
The new node's next should point to the current head of the list.
3fill in blank
hardFix the error in updating the previous pointer of the old head node.
DSA Javascript
function insertAtBeginning(head, data) {
const newNode = new Node(data);
newNode.next = head;
if (head !== null) {
head.[1] = newNode;
}
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'prev'.
Using incorrect property names like 'previous' or 'pointer'.
✗ Incorrect
The previous pointer of the old head should point to the new node.
4fill in blank
hardFill both blanks to complete the function that inserts a node at the beginning of the doubly linked list.
DSA Javascript
function insertAtBeginning(head, data) {
const newNode = new Node(data);
newNode.[1] = head;
if (head !== null) {
head.[2] = newNode;
}
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'next' and 'prev' pointers.
Using incorrect property names.
✗ Incorrect
The new node's next points to the old head, and the old head's prev points back to the new node.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning function with proper pointer updates.
DSA Javascript
function insertAtBeginning(head, data) {
const newNode = new Node(data);
newNode.[1] = head;
newNode.[2] = null;
if (head !== null) {
head.[3] = newNode;
}
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not setting newNode.prev to null.
Not updating head.prev correctly.
Mixing up next and prev pointers.
✗ Incorrect
The new node's next points to the old head, its prev is null because it becomes the first node, and the old head's prev points back to the new node.
