class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } 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; } else { this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseForward() { 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.traverseForward();
The forward traversal starts at the head node (10) and moves through next pointers: 10 to 20 to 30, then null.
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } 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; } else { this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseBackward() { let current = this.tail; let result = ''; while (current) { result += current.value + ' -> '; current = current.prev; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(5); list.append(15); list.append(25); list.traverseBackward();
The backward traversal starts at the tail node (25) and moves through prev pointers: 25 to 15 to 5, then null.
In a doubly linked list, each node has a next pointer to the next node and a prev pointer to the previous node. Forward traversal follows next pointers from head to tail, backward traversal follows prev pointers from tail to head.
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } 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; } else { this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseBackward() { let current = this.head; // Bug here: should start from tail let result = ''; while (current) { result += current.value + ' -> '; current = current.prev; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(1); list.append(2); list.append(3); list.traverseBackward();
The traversal starts at head but moves using prev pointers which are null at head, so it prints only the head value then stops.
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } 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; } else { this.tail!.next = newNode; newNode.prev = this.tail; this.tail = newNode; } } traverseMixed() { let current = this.head; let result = ''; while (current && current.next) { result += current.value + ' -> '; current = current.next; } // Now traverse backward one step if (current) { result += current.value + ' -> '; current = current.prev; } if (current) { result += current.value + ' -> '; } result += 'null'; console.log(result); } } const list = new DoublyLinkedList(); list.append(7); list.append(14); list.append(21); list.traverseMixed();
The code moves forward from head to tail printing 7, 14, 21, then moves one step backward to 14 and prints it before ending.
