|
| 1 | +class Node<T> { |
| 2 | + value: T; |
| 3 | + next: Node<T> | null = null; |
| 4 | + constructor(value: T) { |
| 5 | + this.value = value; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +class LinkedList<T> { |
| 10 | + private head: Node<T> | null = null; |
| 11 | + private length: number = 0; |
| 12 | + |
| 13 | + append(value: T): void { |
| 14 | + const newNode = new Node(value); |
| 15 | + if (!this.head) { |
| 16 | + this.head = newNode; |
| 17 | + } else { |
| 18 | + let current = this.head; |
| 19 | + while (current.next) { |
| 20 | + current = current.next; |
| 21 | + } |
| 22 | + current.next = newNode; |
| 23 | + } |
| 24 | + this.length++; |
| 25 | + } |
| 26 | + |
| 27 | + prepend(value: T): void { |
| 28 | + const newNode = new Node(value); |
| 29 | + newNode.next = this.head; |
| 30 | + this.head = newNode; |
| 31 | + this.length++; |
| 32 | + } |
| 33 | + |
| 34 | + insertAt(index: number, value: T): void { |
| 35 | + if (index < 0 || index > this.length) return console.log("Invalid index"); |
| 36 | + if (index === 0) return this.prepend(value); |
| 37 | + if (index === this.length) return this.append(value); |
| 38 | + |
| 39 | + const newNode = new Node(value); |
| 40 | + let current = this.head; |
| 41 | + let prev: Node<T> | null = null; |
| 42 | + let i = 0; |
| 43 | + while (i < index && current) { |
| 44 | + prev = current; |
| 45 | + current = current.next; |
| 46 | + i++; |
| 47 | + } |
| 48 | + if (prev) { |
| 49 | + newNode.next = current; |
| 50 | + prev.next = newNode; |
| 51 | + this.length++; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + remove(value: T): void { |
| 56 | + if (!this.head) return; |
| 57 | + |
| 58 | + if (this.head.value === value) { |
| 59 | + this.head = this.head.next; |
| 60 | + this.length--; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + let current = this.head; |
| 65 | + let prev: Node<T> | null = null; |
| 66 | + while (current && current.value !== value) { |
| 67 | + prev = current; |
| 68 | + current = current.next; |
| 69 | + } |
| 70 | + |
| 71 | + if (current && prev) { |
| 72 | + prev.next = current.next; |
| 73 | + this.length--; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + search(value: T): number { |
| 78 | + let current = this.head; |
| 79 | + let index = 0; |
| 80 | + while (current) { |
| 81 | + if (current.value === value) return index; |
| 82 | + current = current.next; |
| 83 | + index++; |
| 84 | + } |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + printList(): void { |
| 89 | + let current = this.head; |
| 90 | + let result = ""; |
| 91 | + while (current) { |
| 92 | + result += current.value + " -> "; |
| 93 | + current = current.next; |
| 94 | + } |
| 95 | + console.log(result + "null"); |
| 96 | + } |
| 97 | + |
| 98 | + size(): number { |
| 99 | + return this.length; |
| 100 | + } |
| 101 | + |
| 102 | + isEmpty(): boolean { |
| 103 | + return this.length === 0; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Example usage: |
| 108 | +const list = new LinkedList<number>(); |
| 109 | +list.append(1); |
| 110 | +list.append(2); |
| 111 | +list.append(3); |
| 112 | +list.prepend(0); |
| 113 | +list.printList(); // Output: 0 -> 1 -> 2 -> 3 -> null |
| 114 | +console.log(list.search(2)); // Output: 2 |
| 115 | +list.remove(1); |
| 116 | +list.printList(); // Output: 0 -> 2 -> 3 -> null |
| 117 | +console.log(list.size()); // Output: 3 |
0 commit comments