fix(curriculum): added new test and corrected existing solution for Remove Elements from a Linked List challenge (#36121)

* fix: added new test and corrected solution

* fix: corrected test

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: corrected test

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: allow last element to be removed

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Randell Dawson
2019-06-04 11:55:50 -07:00
committed by Manish Giri
parent 6feda1badb
commit da610150ad

View File

@ -25,13 +25,15 @@ The <code>length</code> of the list should decrease by one every time an element
```yml ```yml
tests: tests:
- text: Your <code>LinkedList</code> class should have a <code>remove</code> method. - text: Your <code>LinkedList</code> class should have a <code>remove</code> method.
testString: assert((function(){var test = new LinkedList(); return (typeof test.remove === 'function')}()), 'Your <code>LinkedList</code> class should have a <code>remove</code> method.'); testString: assert((function(){var test = new LinkedList(); return (typeof test.remove === 'function')}()));
- text: Your <code>remove</code> method should reassign <code>head</code> to the second node when the first node is removed. - text: Your <code>remove</code> method should reassign <code>head</code> to the second node when the first node is removed.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.remove('cat'); return test.head().element === 'dog'}()), 'Your <code>remove</code> method should reassign <code>head</code> to the second node when the first node is removed.'); testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.remove('cat'); return test.head().element === 'dog'}()));
- text: Your <code>remove</code> method should decrease the <code>length</code> of the linked list by one for every node removed. - text: Your <code>remove</code> method should decrease the <code>length</code> of the linked list by one for every node removed.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.remove('cat'); return test.size() === 1})(), 'Your <code>remove</code> method should decrease the <code>length</code> of the linked list by one for every node removed.'); testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.remove('cat'); return test.size() === 1})());
- text: Your <code>remove</code> method should reassign the reference of the previous node of the removed node to the removed node&apos;s <code>next</code> reference. - text: Your <code>remove</code> method should reassign the reference of the previous node of the removed node to the removed node&apos;s <code>next</code> reference.
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 <code>remove</code> method should reassign the reference of the previous node of the removed node to the removed node&apos;s <code>next</code> reference.'); 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'})());
- text: Your <code>remove</code> method should not change the linked list if the element does not exist in the linked list.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog');test.add('kitten'); test.remove('elephant'); return JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'})());
``` ```
@ -87,73 +89,69 @@ function LinkedList() {
</div> </div>
</section> </section>
## Solution ## Solution
<section id='solution'> <section id='solution'>
```js ```js
class Node { function LinkedList() {
constructor (element) { var length = 0;
var head = null;
var Node = function(element){
this.element = element; this.element = element;
this.next = null; this.next = null;
} };
}
class LinkedList { this.size = function(){
constructor () { return length;
this._length = 0; };
this._head = null;
}
head () { this.head = function(){
return this._head; return head;
} };
size () { this.add = function(element){
return this._length; var node = new Node(element);
} if(head === null){
head = node;
add (element) {
const node = new Node(element);
if (this._head === null) {
this._head = node;
} else { } else {
let current = this._head; var currentNode = head;
while (current.next !== null) { while(currentNode.next){
current = current.next; currentNode = currentNode.next;
} }
current.next = node; currentNode.next = node;
} }
++this._length; length++;
} };
remove (element) { this.remove = function(element){
if (this._head === null) return; if (head === null) {
return;
}
var previous;
var currentNode = head;
let previous; while (currentNode.next !== null && currentNode.element !== element) {
let current = this._head; previous = currentNode;
currentNode = currentNode.next;
while (current.next !== null && current.element !== element) {
previous = current;
current = current.next;
} }
if (previous) { if (currentNode.next === null && currentNode.element !== element) {
previous.next = current.next; return;
}
else if (previous) {
previous.next = currentNode.next;
} else { } else {
this._head = current.next; head = currentNode.next;
} }
--this._length; length--;
} };
} }
``` ```