Data Structures: Create a Linked List Class
This commit is contained in:
parent
e33373fa00
commit
55c2581f40
35
Data Structures/linkedListClassAdd.js
Normal file
35
Data Structures/linkedListClassAdd.js
Normal file
@ -0,0 +1,35 @@
|
||||
// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-linked-list-class
|
||||
|
||||
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
|
||||
const newNode = new Node(element);
|
||||
length++;
|
||||
let current = head;
|
||||
if (current === null) {
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
while (current.next !== null) {
|
||||
current = current.next;
|
||||
}
|
||||
current.next = newNode;
|
||||
// Only change code above this line
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user