Adding a solution for reversing a DoubleLinkedList. (#38427)

This commit is contained in:
Keith Warter 2020-03-26 21:01:49 -07:00 committed by GitHub
parent 06d97ac813
commit 3ee1c39e41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,7 +122,35 @@ DoublyLinkedList.prototype = Object.assign(
<section id='solution'>
```js
// solution required
var Node = function(data, prev) {
this.data = data;
this.prev = prev;
this.next = null;
};
var DoublyLinkedList = function() {
this.head = null;
this.tail = null;
this.reverse = function() {
if (!this.head || !this.head.next) {
return this.head
}
let tail;
let temp;
let current = this.head;
while(current !== null) {
if(!tail) tail = current;
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
this.head = temp.prev;
this.tail = tail
}
};
```
</section>