diff --git a/Data Structures/doublyLinkedListReverse.js b/Data Structures/doublyLinkedListReverse.js new file mode 100644 index 0000000..cf3f251 --- /dev/null +++ b/Data Structures/doublyLinkedListReverse.js @@ -0,0 +1,25 @@ +// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/reverse-a-doubly-linked-list + +var Node = function (data, prev) { + this.data = data; + this.prev = prev; + this.next = null; +}; +var DoublyLinkedList = function () { + this.head = null; + this.tail = null; + // Only change code below this line + this.reverse = () => { + const newTail = this.head; + let node = this.head; + while (node !== null) { + const newPrev = node.next; + node.next = node.prev; + node.prev = newPrev; + node = newPrev; + } + this.head = this.tail; + this.tail = newTail; + }; + // Only change code above this line +};