diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-linked-list-class.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-linked-list-class.english.md index a8ad4be129..6409f7c80f 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-linked-list-class.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/create-a-linked-list-class.english.md @@ -78,6 +78,40 @@ function LinkedList() {
```js -// solution required +function LinkedList() { + var length = 0; + var head = null; + + var Node = function(element){ + this.element = element; + this.next = null; + }; + + this.head = function(){ + return head; + }; + + this.size = function(){ + return length; + }; + + this.add = function(element){ + // Only change code below this line + if (head == null) { + head = new Node(element); + } + else { + let currentNode = head; + while (currentNode.next != null) { + // currentNode.next will be last node of linked list after loop + currentNode = currentNode.next; + } + currentNode.next = new Node(element); + } + length++; + // Only change code above this line + }; +} ``` +