Adding a solution for reversing a DoubleLinkedList. (#38427)
This commit is contained in:
parent
06d97ac813
commit
3ee1c39e41
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user