Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with a given value in a doubly linked list.
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; this.[1] = null; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'previousNode' or 'back' instead of the standard 'prev' property name.
✗ Incorrect
In a doubly linked list, each node has a 'prev' pointer to the previous node.
2fill in blank
mediumComplete the code to add a new node at the end of the doubly linked list.
DSA Javascript
addToEnd(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
newNode.[1] = this.tail;
this.tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like 'previous' or 'last' which do not exist.
✗ Incorrect
The new node's 'prev' pointer should point to the current tail before updating the tail.
3fill in blank
hardFix the error in the remove method to correctly update the previous node's next pointer.
DSA Javascript
remove(node) {
if (node.prev) {
node.prev.[1] = node.next;
} else {
this.head = node.next;
}
if (node.next) {
node.next.prev = node.prev;
} else {
this.tail = node.prev;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting node.prev.prev instead of node.prev.next causes incorrect linkage.
✗ Incorrect
To remove a node, the previous node's 'next' pointer must skip the removed node and point to the removed node's next.
4fill in blank
hardFill both blanks to correctly insert a new node after a given node in the doubly linked list.
DSA Javascript
insertAfter(node, value) {
const newNode = new Node(value);
newNode.next = node.[1];
newNode.[2] = node;
if (node.next) {
node.next.prev = newNode;
} else {
this.tail = newNode;
}
node.next = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'next' and 'prev' pointers or using incorrect property names.
✗ Incorrect
The new node's 'next' points to the original next node, and its 'prev' points to the current node.
5fill in blank
hardFill all three blanks to correctly traverse the doubly linked list from head to tail and collect values.
DSA Javascript
traverse() {
let current = this.[1];
const values = [];
while (current !== null) {
values.push(current.[2]);
current = current.[3];
}
return values;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from tail or accessing wrong properties like 'prev' instead of 'next'.
✗ Incorrect
Start from head, collect each node's value, and move to the next node until the end.
