Complete the code to create a linked list node with a value and a pointer to the next node.
function ListNode(value) {
this.value = value;
this.next = [1];
}The next pointer should start as null because the node does not point to any other node yet.
Complete the code to add a new node at the end of a linked list.
function appendNode(head, value) {
let newNode = new ListNode(value);
let current = head;
while (current.[1] !== null) {
current = current.next;
}
current.next = newNode;
}We check current.next to find the last node whose next is null.
Fix the error in the code that tries to insert a node after a given node.
function insertAfter(node, value) {
let newNode = new ListNode(value);
newNode.next = node.[1];
node.next = newNode;
}To insert after the given node, the new node's next should point to the current node's next.
Fill both blanks to create a function that counts the number of nodes in a linked list.
function countNodes(head) {
let count = 0;
let current = head;
while (current [1] null) {
count [2] 1;
current = current.next;
}
return count;
}The loop continues while current is not null, and count increases by 1 each step.
Fill all three blanks to create a function that finds a node with a given value in a linked list.
function findNode(head, target) {
let current = head;
while (current !== null) {
if (current.[1] === [2]) {
return current;
}
current = current.[3];
}
return null;
}We compare the current node's value to the target, and move to the next node if not found.
