Back to Freecodecamp

Reverse a Doubly Linked List

curriculum/challenges/english/blocks/data-structures/587d825a367417b2b2512c88.md

latest3.5 KB
Original Source

--description--

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();
    }
    var n1 = new Node(58, null);
    var n2 = new Node(61, n1);
    var n3 = new Node(32, n2);
    var n4 = new Node(95, n3);
    var n5 = new Node(41, n4);
    n1.next = n2;
    n2.next = n3;
    n3.next = n4;
    n4.next = n5;
    test.head = n1;
    test.tail = n5;
    test.reverse();

    var result = [];
    var current = test.head;
    while (current) {
      result.push(current.data);
      current = current.next;
    }

    return result.join('') == '4195326158';
  })()
);

The next and previous references should be correctly maintained when a list is reversed.

js
assert(
  (function () {
    var test = false;
    if (typeof DoublyLinkedList !== 'undefined') {
      test = new DoublyLinkedList();
    }
    var n1 = new Node(11, null);
    var n2 = new Node(22, n1);
    var n3 = new Node(33, n2);
    var n4 = new Node(44, n3);
    var n5 = new Node(55, n4);
    n1.next = n2;
    n2.next = n3;
    n3.next = n4;
    n4.next = n5;
    test.head = n1;
    test.tail = n5;
    test.reverse();

    var result = [];
    var current = test.tail;
    while (current) {
      result.push(current.data);
      current = current.prev;
    }

    return result.join('') == '1122334455';
  })()
);

--seed--

--seed-contents--

js
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
  
  // Only change code above this line
};

--solutions--

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