--- id: 587d8251367417b2b2512c63 title: Remove Elements from a Linked List challengeType: 1 forumTopicId: 301712 localeTitle: Удалить элементы из связанного списка --- ## Description
Следующим важным методом, который потребуется для любой реализации связанного списка, является метод remove . Этот метод должен взять элемент, который мы хотим удалить в качестве аргумента, а затем выполнить поиск в списке, чтобы найти и удалить узел, содержащий этот элемент. Всякий раз, когда мы удаляем узел из связанного списка, важно, чтобы мы не случайно оставили все остальное в списке. Напомним, что next свойство next узла указывает на узел, который следует за ним в списке. Если мы удалим средний элемент, скажем, мы хотим убедиться , что у нас есть связь с предыдущим узлом этого элемента next имущества к середине элемента next имуществу (который является следующим узлом в списке!) Это может показаться действительно запутанный, так что давайте вернемся к примеру линии conga, чтобы у нас была хорошая концептуальная модель. Представьте себя в линии конги, и человек прямо перед вами покинет линию. Лицо, которое только что покинуло линию, больше не имеет руки на кого-либо в очереди - и у вас больше нет рук на лице, которое осталось. Вы шагнете вперед и положите руки на следующего человека, которого видите. Если элемент мы хотим , чтобы удалить это head элемент, мы переназначить head на второй узел связанного списка.
## Instructions
Напишите метод remove который принимает элемент и удаляет его из связанного списка. Примечание. length списка должна уменьшаться на единицу при каждом удалении элемента из связанного списка.
## Tests
```yml tests: - text: 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'}())); - 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})()); - 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'})()); - 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}}}'})()); ```
## Challenge Seed
```js function LinkedList() { var length = 0; var head = null; var Node = function(element){ this.element = element; this.next = null; }; this.size = function(){ return length; }; this.head = function(){ return head; }; this.add = function(element){ var node = new Node(element); if(head === null){ head = node; } else { var currentNode = head; while(currentNode.next){ currentNode = currentNode.next; } currentNode.next = node; } length++; }; this.remove = function(element){ // Only change code below this line // Only change code above this line }; } ```
## Solution
```js function LinkedList() { var length = 0; var head = null; var Node = function(element){ this.element = element; this.next = null; }; this.size = function(){ return length; }; this.head = function(){ return head; }; this.add = function(element){ var node = new Node(element); if(head === null){ head = node; } else { var currentNode = head; while(currentNode.next){ currentNode = currentNode.next; } currentNode.next = node; } length++; }; this.remove = function(element){ if (head === null) { return; } var previous; var currentNode = head; 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 { head = currentNode.next; } length--; }; } ```