remove
方法。此方法应将要删除的元素作为参数,然后搜索列表以查找并删除包含该元素的节点。每当我们从链表中删除一个节点时,重要的是我们不要意外地孤立列表的其余部分。回想一下,每个节点的next
属性都指向列表中跟随它的节点。如果我们删除中间元素,比如说,我们要确保我们从该元素的前一个节点的next
属性到中间元素的next
属性(这是列表中的下一个节点)的连接!这可能听起来真的很混乱,所以让我们回到康加线的例子,这样我们就有了一个很好的概念模型。想象自己在康加舞线上,直接在你面前的人离开了这条线。刚离开生产线的人不再将手放在任何人身上 - 而且你不再把手放在离开的人身上。你向前走,把你的手放在你看到的下一个人身上。如果我们要删除的元素是head
元素,我们将head
重新分配给链表的第二个节点。 remove
方法,该方法接受一个元素并将其从链表中删除。注意每次从链接列表中删除元素时,列表的length
应减少一。 LinkedList
类应该有一个remove
方法。
testString: 'assert((function(){var test = new LinkedList(); return (typeof test.remove === "function")}()), "Your LinkedList
class should have a remove
method.");'
- text: remove
第一个节点时, remove
方法应重新分配head
到第二个节点。
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.remove("cat"); return test.head().element === "dog"}()), "Your remove
method should reassign head
to the second node when the first node is removed.");'
- text: 对于每个删除的节点,您的remove
方法应该将链表的length
减少一个。
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog"); test.remove("cat"); return test.size() === 1})(), "Your remove
method should decrease the length
of the linked list by one for every node removed.");'
- text: 您的remove
方法应该将已删除节点的上next
节点的引用重新分配给已删除节点的next
引用。
testString: 'assert((function(){var test = new LinkedList(); test.add("cat"); test.add("dog");test.add("kitten"); test.remove("dog"); return test.head().next.element === "kitten"})(), "Your remove
method should reassign the reference of the previous node of the removed node to the removed node's next
reference.");'
```