freeCodeCamp/Data Structures/doublyLinkedListReverse.js

26 lines
644 B
JavaScript
Raw Permalink Normal View History

// 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
};