Reverse a Doubly Linked List

Reverse a Doubly Linked List

Tell us what’s happening:
Test output:

// running tests
Cannot read property 'join' of undefined
// tests completed

My own quick console.log bits give the impression i’m successfully reversing the list. I’m passing the
The next and previous references are correctly maintained when a list is reversed.
test, but somehow failing
The reverse method reverses the list.
I’m quite lost on how I could have successfully updated the next and previous references in a reversed list but also fail to reverse the list (according to the test case). The actual test output doesn’t help much (perhaps there’s some other specific test data I can/should use to point me in the right direction?).

Your code so far

var Node = function(data, prev) { = data;
  this.prev = prev; = null;
var DoublyLinkedList = function() {
  this.head = null;
  this.tail = null;
  // change code below this line
  this.reverse = function () {
    if(!this.head) return null;

    let node = this.head;
    this.head = this.tail;
    this.tail = node;

    while(node) {
      let hold =; = node.prev;
      node.prev = hold;
      node = node.prev;

    // console.log('head',
    // console.log('tail',

  this.add = function(element) {
    if(!this.head) {
      this.head = new Node(element, null)
      this.tail = this.head;
    } else { = new Node(element, this.tail);
      this.tail =;

  this.print = () => {
    let node = this.head;
    while(node) {
      node =;
  // change code above this line
var x = new DoublyLinkedList();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36.

Link to the challenge:

Based off the wording in the challenge and the tests, I believe that reverse should return a reversed version of the list, but it should not actually change any of the list nodes.

Really? I read the text

reverses the list in place

as suggesting mutation. The only explicitly mentioned return value in the description or test cases is in the specific instance that one is

Trying to reverse an empty list should return null.

You could certainly be right, but I’m not following what part of the text gives you the impression that one is not expected to mutate the list.

Sorry. I skimmed while distracted. It looks like you should be modifying the linked list.