diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md index 483c7ed822..92caa079bb 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md @@ -15,8 +15,7 @@ Just like our remove(element) method, which removeAt method should remove and return the element at specified index, and reduce the length of the linked list. + testString: assert((function(){var test = new LinkedList(); test.add('cat'); var removedItem = test.removeAt(0); return test.head() === null && test.size() === 0 && removedItem === 'cat';}())); + - text: Your removeAt method should return the element of the removed node. + testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.removeAt(1) === 'dog'}())); + - text: Your removeAt method should return null and the linked list should not change if the given index is less than 0. + testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); var removedItem = test.removeAt(-1); return removedItem === null && JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'}())); + - text: Your removeAt method should return null and the linked list should not change if the given index is greater than or equal to the length of the list. + testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); var removedItem = test.removeAt(3); return removedItem === null && JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'}())); ``` @@ -85,14 +88,13 @@ function LinkedList() { ``` + ## Solution
```js - - function LinkedList() { var length = 0; var head = null; @@ -137,8 +139,6 @@ function LinkedList() { if (index === 0) { var removed = head.element; head = currentNode.next; - length--; - return removed; } else { while (count < index) { previous = currentNode; @@ -147,12 +147,11 @@ function LinkedList() { } var removed = previous.next.element; previous.next = currentNode.next; - length--; - return removed; } + length--; + return removed; }; } - - ``` +