class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(10); list.append(20); list.append(30); list.printList();
The append method adds nodes to the end of the list. The printList method prints values from head to tail separated by ' <-> '. So the output shows 10 linked to 20 linked to 30, ending with null.
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } removeHead() { if (!this.head) return; if (this.head === this.tail) { this.head = null; this.tail = null; return; } this.head = this.head.next; if (this.head) this.head.prev = null; } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(5); list.append(15); list.append(25); list.removeHead(); list.printList();
After removing the head (5), the new head is 15. The list prints from 15 to 25 ending with null.
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } const list = new DoublyLinkedList(); list.append(1); list.append(2);
The code uses 'this.tail.next' without checking if 'this.tail' is null. On the first append, tail is set, but on the second append, if tail was null, accessing 'next' causes a TypeError.
class Node { value: number; next: Node | null = null; prev: Node | null = null; constructor(value: number) { this.value = value; } } class DoublyLinkedList { head: Node | null = null; tail: Node | null = null; append(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; return; } this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } reverse() { let current = this.head; let temp: Node | null = null; while (current) { temp = current.prev; current.prev = current.next; current.next = temp; current = current.prev; } if (temp) { this.head = temp.prev; } } printList() { let current = this.head; let result = ''; while (current) { result += current.value + ' <-> '; current = current.next; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(1); list.append(2); list.append(3); list.reverse(); list.printList();
The reverse method swaps the next and prev pointers of each node and then updates the head to the last node processed. This reverses the list order.
Operations step-by-step:
append(4) → list: 4
append(7) → list: 4 <-> 7
append(9) → list: 4 <-> 7 <-> 9
removeHead() removes 4 → list: 7 <-> 9
append(10) → list: 7 <-> 9 <-> 10
removeTail() removes 10 → list: 7 <-> 9
Remaining nodes: 2
