freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.md
Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

4.8 KiB
Raw Blame History

id, title, challengeType, videoUrl, dashedName
id title challengeType videoUrl dashedName
587d8251367417b2b2512c65 按索引从链接列表中删除元素 1 remove-elements-from-a-linked-list-by-index

--description--

在我们继续讨论另一个数据结构之前,让我们先了解链接列表的最后几点练习。让我们编写一个removeAt方法,删除给定index处的element 。该方法应该称为removeAt(index) 。要删除某个index处的element ,我们需要在沿着链表移动时保持每个节点的运行计数。用于遍历链表的元素的常用技术涉及“转轮”或“哨兵”,它们“指向”代码所比较的节点。在我们的情况下,开始于head我们的名单中,我们先从一个currentIndex始于变量0 。对于我们传递的每个节点, currentIndex应该增加1。就像我们的remove(element)方法一样当我们在removeAtindex方法中删除节点时我们需要注意不要孤立列表的其余部分。我们通过确保引用已删除节点的节点具有对下一节点的引用来保持节点连续。

--instructions--

编写removeAt(index)方法,删除并返回给定index处的节点。如果给定index为负数,或者大于或等于链表length ,则该方法应返回null 。注意请记住保持currentIndex计数。

--hints--

您的LinkedList类应该有一个removeAt方法。

assert(
  (function () {
    var test = new LinkedList();
    return typeof test.removeAt === 'function';
  })(),
  'Your <code>LinkedList</code> class should have a <code>removeAt</code> method.'
);

您的removeAt方法应该减少链表的length

assert(
  (function () {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    test.removeAt(1);
    return test.size() === 2;
  })(),
  'Your <code>removeAt</code> method should reduce the <code>length</code> of the linked list'
);

您的removeAt方法还应该返回已删除节点的元素。

assert(
  (function () {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    return test.removeAt(1) === 'dog';
  })(),
  'Your <code>removeAt</code> method should also return the element of the removed node.'
);

如果给定索引小于0removeAt方法也应返回null

assert(
  (function () {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    return test.removeAt(-1) === null;
  })(),
  'Your <code>removeAt</code> method should also return <code>null</code> if the given index is less than <code>0</code>'
);

如果给定索引等于或大于链表的length ,则removeAt方法也应返回null

assert(
  (function () {
    var test = new LinkedList();
    test.add('cat');
    test.add('dog');
    test.add('kitten');
    return test.removeAt(3) === null;
  })(),
  'Your <code>removeAt</code> method should also return <code>null</code> if the given index is equal or more than the <code>length</code> of the linked list.'
);

--seed--

--seed-contents--

function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function(element){
    this.element = element;
    this.next = null;
  };

  this.size = function(){
    return length;
  };

  this.head = function(){
    return head;
  };

  this.add = function(element){
    var node = new Node(element);
    if(head === null){
      head = node;
    } else {
      var currentNode = head;

      while(currentNode.next){
        currentNode  = currentNode.next;
      }

      currentNode.next = node;
    }

    length++;
  };

  // Only change code below this line

  // Only change code above this line
}

--solutions--

function LinkedList() {
  var length = 0;
  var head = null;

  var Node = function (element) {
    this.element = element;
    this.next = null;
  };

  this.size = function () {
    return length;
  };

  this.head = function () {
    return head;
  };

  this.add = function (element) {
    var node = new Node(element);
    if (head === null) {
      head = node;
    } else {
      var currentNode = head;

      while (currentNode.next) {
        currentNode = currentNode.next;
      }

      currentNode.next = node;
    }

    length++;
  };

  this.removeAt = function (index) {
    var currentNode = head;
    var previous = head;
    var count = 0;
    if (index >= length || index < 0) {
      return null;
    }
    if (index === 0) {
      var removed = head.element;
      head = currentNode.next;
    } else {
      while (count < index) {
        previous = currentNode;
        currentNode = currentNode.next;
        count++;
      }
      var removed = previous.next.element;
      previous.next = currentNode.next;
    }
    length--;
    return removed;
  };
}