freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/data-structures/reverse-a-doubly-linked-lis...

3.0 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d825a367417b2b2512c88 Reverse a Doubly Linked List 1 反转双重链接列表

Description

让我们为我们的双向链表创建一个名为reverse的方法它可以反转列表。一旦执行该方法头部应指向前一个尾部尾部应指向前一个头部。现在如果我们从头到尾遍历列表我们应该以与原始列表相反的顺序来满足节点。尝试反转空列表应返回null。

Instructions

Tests

tests:
  - text: 存在DoublyLinkedList数据结构。
    testString: 'assert((function() { var test = false; if (typeof DoublyLinkedList !== "undefined") { test = new DoublyLinkedList() }; return (typeof test == "object")})(), "The DoublyLinkedList data structure exists.");'
  - text: DoublyLinkedList有一个名为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")})(), "The DoublyLinkedList has a method called add.");'
  - text: DoublyLinkedList有一个名为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")})(), "The DoublyLinkedList has a method called reverse.");'
  - text: 反转空列表将返回null。
    testString: 'assert((function() { var test = false; if (typeof DoublyLinkedList !== "undefined") { test = new DoublyLinkedList() }; return (test.reverse() == null); })(), "Reversing an empty list returns null.");'
  - text: 反向方法反转列表。
    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"); })(), "The reverse method reverses the list.");'
  - text: 当列表反转时,正确维护下一个和上一个引用。
    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"); })(), "The next and previous references are correctly maintained when a list is reversed.");'

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

console.info('after the test');

Solution

// solution required