JSFiddle - React, Tailwind, and code Playground
by Jagrati Tomar
JavaScript
var Node = function(x) {
this.data = x
this.next = null
}
var myList = (function LinkedList() {
var head = null
return {
add : function(x) {
let node = new Node(x)
if (!head) {
head = node
} else {
let temp = head
while(temp.next != null) {
temp = temp.next
}
temp.next = node
}
},
printList : function () {
temp = head
while (temp.next != null) {
console.log(temp.data)
temp = temp.next
}
}
}
})()
myList.add(10)
myList.add(20)
myList.printList()