From da610150adf83c75a60e2fb1cd701bf73e216b21 Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Tue, 4 Jun 2019 11:55:50 -0700 Subject: [PATCH] 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 * fix: corrected test Co-Authored-By: Oliver Eyton-Williams * fix: allow last element to be removed Co-Authored-By: Oliver Eyton-Williams --- ...ove-elements-from-a-linked-list.english.md | 98 +++++++++---------- 1 file changed, 48 insertions(+), 50 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list.english.md index 16e271efb9..ec9df2029a 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list.english.md @@ -25,13 +25,15 @@ The length of the list should decrease by one every time an element ```yml tests: - text: Your LinkedList class should have a remove method. - testString: assert((function(){var test = new LinkedList(); return (typeof test.remove === 'function')}()), 'Your LinkedList class should have a remove method.'); + testString: assert((function(){var test = new LinkedList(); return (typeof test.remove === 'function')}())); - text: Your remove method should reassign head 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 remove method should reassign head 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 remove method should decrease the length 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 remove method should decrease the length 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 remove method should reassign the reference of the previous node of the removed node to the removed node's next 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 remove method should reassign the reference of the previous node of the removed node to the removed node's next 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 remove 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,74 +89,70 @@ function LinkedList() { - - ## Solution
- ```js -class Node { - constructor (element) { +function LinkedList() { + var length = 0; + var head = null; + + var Node = function(element){ this.element = element; this.next = null; - } -} + }; -class LinkedList { - constructor () { - this._length = 0; - this._head = null; - } + this.size = function(){ + return length; + }; - head () { - return this._head; - } + this.head = function(){ + return head; + }; - size () { - return this._length; - } - - add (element) { - const node = new Node(element); - - if (this._head === null) { - this._head = node; + this.add = function(element){ + var node = new Node(element); + if(head === null){ + head = node; } else { - let current = this._head; + var currentNode = head; - while (current.next !== null) { - current = current.next; - } + while(currentNode.next){ + currentNode = currentNode.next; + } - current.next = node; + currentNode.next = node; } - ++this._length; - } + length++; + }; - remove (element) { - if (this._head === null) return; - - let previous; - let current = this._head; - - while (current.next !== null && current.element !== element) { - previous = current; - current = current.next; + this.remove = function(element){ + if (head === null) { + return; } + var previous; + var currentNode = head; - if (previous) { - previous.next = current.next; + while (currentNode.next !== null && currentNode.element !== element) { + previous = currentNode; + currentNode = currentNode.next; + } + + if (currentNode.next === null && currentNode.element !== element) { + return; + } + else if (previous) { + previous.next = currentNode.next; } else { - this._head = current.next; + head = currentNode.next; } - --this._length; - } -} + length--; + }; +} ```