Randell Dawson c25fa49b5b fix(curriculum): changed test text to use should for Coding Interview Prep - part 1 of 2 (#37765)
* fix: changed test text to use should

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: use singular of verb

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: changed punctuation

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: reworded test text

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
2019-11-19 20:13:45 -05:00

3.8 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d825a367417b2b2512c88 Reverse a Doubly Linked List 1 301714

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.

Instructions

Tests

tests:
  - text: The DoublyLinkedList data structure should exist.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (typeof test == 'object')})());
  - text: The DoublyLinkedList should have a method called add.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.add == undefined) { return false; }; return (typeof test.add == 'function')})());
  - text: The DoublyLinkedList should have a method called reverse.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; if (test.reverse == undefined) { return false; }; return (typeof test.reverse == 'function')})());
  - text: Reversing an empty list should return null.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; return (test.reverse() == null); })());
  - text: The reverse method should reverse the list.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(58); test.add(61); test.add(32); test.reverse(); return (test.print().join('') == '326158'); })());
  - text: The next and previous references should be correctly maintained when a list is reversed.
    testString: assert((function() { var test = false; if (typeof DoublyLinkedList !== 'undefined') { test = new DoublyLinkedList() }; test.add(11); test.add(22); test.add(33); test.reverse(); return (test.printReverse().join('') == '112233'); })());

Challenge Seed

var Node = function(data, prev) {
  this.data = data;
  this.prev = prev;
  this.next = null;
};
var DoublyLinkedList = function() {
  this.head = null;
  this.tail = null;
  // change code below this line
  // change code above this line
};

After Test

DoublyLinkedList.prototype = {
  add(data) {
    if (this.head == null) {
      this.head = new Node(data, null);
      this.tail = this.head;
    } else {
      var node = this.head;
      var prev = null;
      while (node.next != null) {
        prev = node;
        node = node.next;
      };
      var newNode = new Node(data, node);
      node.next = newNode;
      this.tail = newNode;
    };
  },
  print() {
    if (this.head == null) {
      return null;
    } else {
      var result = new Array();
      var node = this.head;
      while (node.next != null) {
        result.push(node.data);
        node = node.next;
      };
      result.push(node.data);
      return result;
    };
  },
  printReverse() {
    if (this.tail == null) {
      return null;
    } else {
      var result = new Array();
      var node = this.tail;
      while (node.prev != null) {
        result.push(node.data);
        node = node.prev;
      };
      result.push(node.data);
      return result;
    };
  }
};

Solution

// solution required