From 204ece1c7073f1728b722725b66142c7a54e903c Mon Sep 17 00:00:00 2001 From: Stan Choi <43020892+StanimalTheMan@users.noreply.github.com> Date: Thu, 20 Jun 2019 11:11:26 -0400 Subject: [PATCH] fix: Add Solution in Curriculum/Challenges/English/08/CodingInterviewProblems/DataStructures/CREATE_LINKED_LIST_CLASS.md (#35819) * fix: add solution create linked list class * fix: added extra line to avoid linting error * fix: renamed some variables --- .../create-a-linked-list-class.english.md | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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 + }; +} ``` +