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 = [1];
this.next = null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'value' for data.
Setting data to null instead of the value.
✗ Incorrect
The new node's data should be set to the given value parameter.
2fill in blank
mediumComplete the code to handle the case when the list is empty (head is null).
DSA Javascript
if (head === null) { newNode.next = [1]; head = newNode; return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to null breaks the circular property.
Setting next to head when head is null causes errors.
✗ Incorrect
In a circular linked list with one node, the node points to itself.
3fill in blank
hardFix the error in the loop that finds the last node in the circular linked list.
DSA Javascript
let current = head; while (current.next !== [1]) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for null instead of head causes infinite loops.
Using newNode instead of head in the condition.
✗ Incorrect
The last node points back to head in a circular linked list.
4fill in blank
hardFill both blanks to insert the new node at the beginning and update the last node's next pointer.
DSA Javascript
current.next = [1]; newNode.next = [2]; return newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to current instead of head.
Not updating current.next to newNode.
✗ Incorrect
The last node's next should point to the newNode, and newNode.next should point to the old head.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning function for a circular linked list.
DSA Javascript
function insertAtBeginning(head, value) {
const newNode = new Node([1]);
if (head === null) {
newNode.next = [2];
return newNode;
}
let current = head;
while (current.next !== [3]) {
current = current.next;
}
current.next = newNode;
newNode.next = head;
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not setting newNode.next to itself when list is empty.
Incorrect loop condition to find last node.
✗ Incorrect
The new node is created with value, points to itself if list is empty, and the last node points to head.
