removeAt
方法,删除给定index
处的element
。该方法应该称为removeAt(index)
。要删除某个index
处的element
,我们需要在沿着链表移动时保持每个节点的运行计数。用于遍历链表的元素的常用技术涉及“转轮”或“哨兵”,它们“指向”代码所比较的节点。在我们的情况下,开始于head
我们的名单中,我们先从一个currentIndex
始于变量0
。对于我们传递的每个节点, currentIndex
应该增加1。就像我们的remove(element)
方法一样,当我们在removeAt(index)方法中删除节点时,我们需要注意不要孤立列表的其余部分。我们通过确保引用已删除节点的节点具有对下一节点的引用来保持节点连续。 removeAt(index)
方法,删除并返回给定index
处的节点。如果给定index
为负数,或者大于或等于链表length
,则该方法应返回null
。注意请记住保持currentIndex
计数。 LinkedList
类应该有一个removeAt
方法。
testString: 'assert((function(){var test = new LinkedList(); return (typeof test.removeAt === "function")}()), "Your LinkedList
class should have a removeAt
method.");'
- text: 您的removeAt
方法应该减少链表的length
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); test.removeAt(1); return test.size() === 2}()), "Your removeAt
method should reduce the length
of the linked list");'
- text: 您的removeAt
方法还应该返回已删除节点的元素。
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return test.removeAt(1) === "dog"}()), "Your removeAt
method should also return the element of the removed node.");'
- text: 如果给定索引小于0
则removeAt
方法也应返回null
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return (test.removeAt(-1) === null)}()), "Your removeAt
method should also return null
if the given index is less than 0
");'
- text: 如果给定索引等于或大于链表的length
,则removeAt
方法也应返回null
。
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.add("kitten"); return (test.removeAt(3) === null)}()), "Your removeAt
method should also return null
if the given index is equal or more than the length
of the linked list.");'
```