2.7 KiB
2.7 KiB
id, challengeType, videoUrl, localeTitle
id | challengeType | videoUrl | localeTitle |
---|---|---|---|
587d8252367417b2b2512c67 | 1 | 在链接列表中的特定索引处添加元素 |
Description
Instructions
Tests
tests:
- text: 当给定索引为0时, <code>addAt</code>方法应重新分配<code>head</code>到新节点。
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.addAt(0,'cat'); return test.head().element === 'cat'}()));
- text: 对于添加到链接列表的每个新节点, <code>addAt</code>方法应该将链表的长度增加一。
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.addAt(0,'cat'); return test.size() === 3}()));
- text: 如果无法添加节点,则<code>addAt</code>方法应返回<code>false</code> 。
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); return (test.addAt(4,'cat') === false); }()));
Challenge Seed
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
}
Solution
// solution required
/section>