Data Structures: Create a Linked List Class

This commit is contained in:
Manish 2023-08-24 13:03:46 +10:00
parent e33373fa00
commit 55c2581f40

View 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
};
}