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
This commit is contained in:
@ -78,6 +78,40 @@ function LinkedList() {
|
||||
<section id='solution'>
|
||||
|
||||
```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
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user