Complete the code to create a new node with the given value.
const newNode = { data: value, next: [1] };The new node's next should point to the head to maintain circularity.
Complete the code to handle the case when the list is empty.
if (head === null) { newNode.next = newNode; head = [1]; return head; }
When the list is empty, head should point to the new node.
Fix the error in the loop that finds the last node.
let current = head; while (current.next !== [1]) { current = current.next; }
The loop should stop when current.next points back to head, the start of the list.
Fill both blanks to insert the new node at the end and maintain circularity.
current.next = [1]; newNode.next = [2];
Set current.next to newNode to add it at the end, and newNode.next to head to keep the circle.
Fill all three blanks to complete the insertAtEnd function for a circular linked list.
function insertAtEnd(head, value) {
const newNode = { data: value, next: [1] };
if (head === null) {
newNode.next = newNode;
head = [2];
return head;
}
let current = head;
while (current.next !== [3]) {
current = current.next;
}
current.next = newNode;
newNode.next = head;
return head;
}The new node's next points to head. If list is empty, head becomes newNode. Loop stops when current.next is head.
