From 55c2581f40753134652e982973bde1f335a411d3 Mon Sep 17 00:00:00 2001 From: Manish Date: Thu, 24 Aug 2023 13:03:46 +1000 Subject: [PATCH] Data Structures: Create a Linked List Class --- Data Structures/linkedListClassAdd.js | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Data Structures/linkedListClassAdd.js diff --git a/Data Structures/linkedListClassAdd.js b/Data Structures/linkedListClassAdd.js new file mode 100644 index 0000000..838af63 --- /dev/null +++ b/Data Structures/linkedListClassAdd.js @@ -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 + }; +}