|
| 1 | +class Node { |
| 2 | + constructor(value) { |
| 3 | + this.value = value; |
| 4 | + this.next = null; |
| 5 | + this.prev = null; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +class LinkedList { |
| 10 | + constructor(value) { |
| 11 | + // create a new node with the value passed to the constructor |
| 12 | + this.head = new Node(value); |
| 13 | + // set the tail to be the same as the head since there is only one node in the linkedlist |
| 14 | + this.tail = this.head; |
| 15 | + this.length = 1; |
| 16 | + } |
| 17 | + |
| 18 | + // append a node to the end of the linkedlist |
| 19 | + append(value) { |
| 20 | + const node = new Node(value); |
| 21 | + // set the next property of the tail to be the new node |
| 22 | + this.tail.next = node; |
| 23 | + // set the previous property of the new node to be the tail |
| 24 | + node.prev = this.tail; |
| 25 | + // set the tail to be the new node |
| 26 | + this.tail = node; |
| 27 | + this.length++; |
| 28 | + return this.print(); |
| 29 | + } |
| 30 | + |
| 31 | + // prepend a node to the beginning of the linkedlist |
| 32 | + prepend(value) { |
| 33 | + const node = new Node(value); |
| 34 | + // set the next property of the new node to be the head |
| 35 | + node.next = this.head; |
| 36 | + // set the previous property of the head to be the new node |
| 37 | + this.head.prev = node; |
| 38 | + // set the head to be the new node |
| 39 | + this.head = node; |
| 40 | + this.length++; |
| 41 | + return this.print(); |
| 42 | + } |
| 43 | + |
| 44 | + // insert a node at a specific index |
| 45 | + insert(index, value) { |
| 46 | + const node = new Node(value); |
| 47 | + // if index is greater than the length of the linkedlist, append the value to the end |
| 48 | + if (index >= this.length) { |
| 49 | + this.append(value); |
| 50 | + return this.print(); |
| 51 | + } |
| 52 | + |
| 53 | + // if index is 0, prepend the value to the beginning |
| 54 | + if (index === 0) { |
| 55 | + this.prepend(value); |
| 56 | + return this.print(); |
| 57 | + } |
| 58 | + |
| 59 | + // if index is in the middle of the linkedlist |
| 60 | + // [1,3,2,5] -- insert(2,55) |
| 61 | + // leadPointer -> 3 |
| 62 | + let leadPointer = this.traverseToIndex(index - 1); |
| 63 | + // trailPointer -> 2 |
| 64 | + let trailPointer = leadPointer.next; |
| 65 | + // set the next of the leadPointer to the new node |
| 66 | + leadPointer.next = node; |
| 67 | + // set the previous of the new node to the leadPointer |
| 68 | + node.prev = leadPointer; |
| 69 | + // set the next of the new node to the trailPointer |
| 70 | + node.next = trailPointer; |
| 71 | + // set the previous of the trailPointer to the new node |
| 72 | + trailPointer.prev = node; |
| 73 | + this.length++; |
| 74 | + return this.print(); |
| 75 | + } |
| 76 | + |
| 77 | + // remove a node at a specific index |
| 78 | + remove(index) { |
| 79 | + // if index is 0, set the head to be the next node |
| 80 | + if (index === 0) { |
| 81 | + this.head = this.head.next; |
| 82 | + this.head.prev = null; |
| 83 | + this.length--; |
| 84 | + return this.print(); |
| 85 | + } |
| 86 | + |
| 87 | + // if index is the last node, set the tail to be the previous node |
| 88 | + if (index >= this.length) { |
| 89 | + this.tail = this.tail.prev; |
| 90 | + this.tail.next = null; |
| 91 | + this.length--; |
| 92 | + return this.print(); |
| 93 | + } |
| 94 | + |
| 95 | + // if index is in the middle of the linkedlist |
| 96 | + // [1,3,2,5] -- remove(2) |
| 97 | + // leadPointer -> 3 |
| 98 | + let leadPointer = this.traverseToIndex(index - 1); |
| 99 | + // trailPointer -> 5 |
| 100 | + let trailPointer = leadPointer.next.next; |
| 101 | + // set the next of the leadPointer to the trailPointer |
| 102 | + leadPointer.next = trailPointer; |
| 103 | + // set the previous of the trailPointer to the leadPointer |
| 104 | + trailPointer.prev = leadPointer; |
| 105 | + this.length--; |
| 106 | + return this.print(); |
| 107 | + } |
| 108 | + |
| 109 | + traverseToIndex(index) { |
| 110 | + let counter = 0; |
| 111 | + let currentNode = this.head; |
| 112 | + while (counter !== index) { |
| 113 | + currentNode = currentNode.next; |
| 114 | + counter++; |
| 115 | + } |
| 116 | + return currentNode; |
| 117 | + } |
| 118 | + |
| 119 | + // print the linkedlist as an array |
| 120 | + print() { |
| 121 | + const array = []; |
| 122 | + // Start with first item in the linkedlist |
| 123 | + let currentNode = this.head; |
| 124 | + while (currentNode !== null) { |
| 125 | + array.push(currentNode.value); |
| 126 | + currentNode = currentNode.next; |
| 127 | + } |
| 128 | + return array; |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Test cases |
| 133 | +const myLinkedList = new LinkedList(10); |
| 134 | +myLinkedList.append(5); |
| 135 | +myLinkedList.append(16); |
| 136 | +myLinkedList.prepend(3); |
| 137 | +myLinkedList.insert(2, 55); |
| 138 | +myLinkedList.insert(3, 45); |
| 139 | +myLinkedList.append(22); |
| 140 | +myLinkedList.prepend(1); |
| 141 | +myLinkedList.insert(0, 0); |
| 142 | +myLinkedList.insert(50, 99); |
| 143 | +myLinkedList.remove(0); |
| 144 | +myLinkedList.remove(50); |
| 145 | +myLinkedList.remove(3); |
| 146 | +myLinkedList.remove(3); |
| 147 | +console.log(myLinkedList.print()); |
0 commit comments