Let's create one more method for our doubly linked list called reverse which reverses the list in place. Once the method is executed the head should point to the previous tail and the tail should point to the previous head. Now, if we traverse the list from head to tail we should meet the nodes in a reverse order compared to the original list. Trying to reverse an empty list should return null.
# --hints--
The DoublyLinkedList data structure should exist.
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return typeof test == 'object';
})()
);
```
The DoublyLinkedList should have a method called reverse.
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
if (test.reverse == undefined) {
return false;
}
return typeof test.reverse == 'function';
})()
);
```
Reversing an empty list should return null.
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
return test.reverse() == null;
})()
);
```
The reverse method should reverse the list.
```js
assert(
(function () {
var test = false;
if (typeof DoublyLinkedList !== 'undefined') {
test = new DoublyLinkedList();
}
test.add(58);
test.add(61);
test.add(32);
test.add(95);
test.add(41);
test.reverse();
return test.print().join('') == '4195326158';
})()
);
```
The next and previous references should be correctly maintained when a list is reversed.