From a6afaf1eb6ac76dcb6bc81df4d967c5be30545be Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Thu, 20 Jun 2019 10:34:08 -0700 Subject: [PATCH] fix(curriculum) - added new tests to prevent invalid solutions in the Remove Elements from a Linked List by Index challenge (#36168) * fix: added new tests to prevent invalid solutions * fix: corrected tests and added solution * fix: added linked word Co-Authored-By: Manish Giri * fix: added by one Co-Authored-By: Manish Giri * fix: removed duplicate code * fix: changed verbiage Co-Authored-By: Manish Giri * fix: added extra line to avoid linting errors --- ...nts-from-a-linked-list-by-index.english.md | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) 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 483c7ed822c..92caa079bb3 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; }; } - - ``` +