Complete the code to create a node with a pointer to the next node only.
class Node { value: number; next: Node | null; constructor(value: number) { 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 previous pointer in a doubly linked list node.
class DoublyNode { value: number; next: DoublyNode | null; prev: DoublyNode | null; constructor(value: number) { this.value = value; this.next = null; this.prev = [1]; } }
The previous pointer should start as null because the node does not have a previous node yet.
Fix the error in the method that moves backward in a doubly linked list.
moveBackward(node: DoublyNode | null): number[] {
const values: number[] = [];
while (node !== null) {
values.push(node.value);
node = node.[1];
}
return values;
}To move backward in a doubly linked list, use the prev pointer.
Fill both blanks to insert a new node after a given node in a doubly linked list.
insertAfter(node: DoublyNode, newNode: DoublyNode): void {
newNode.prev = node;
newNode.next = node.[1];
if (node.[2] !== null) {
node.[2].prev = newNode;
}
node.next = newNode;
}To insert after a node, set newNode's next to node's next, and if node's next exists, update its prev to newNode.
Fill all three blanks to remove a node from a doubly linked list.
remove(node: DoublyNode): void {
if (node.[1] !== null) {
node.[1].prev = node.[2];
}
if (node.[3] !== null) {
node.[3].next = node.[2];
}
}When removing a node, link its next node's prev to its prev, and its prev node's next to its next.
