Linked List
by louis0420
HTML
<pre id="list">
</pre>
JavaScript
class Node{
constructor(data, next = null){
this.data = data,
this.next = next
}
}
class LinkedList{
constructor(){
this.head = null;
}
}
LinkedList.prototype.insertAtBeginning = function(data){
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
return this.head;
}
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8]
let list = new LinkedList();
let i = 0
let j = arr.length
while (i < j) {
list.insertAtBeginning(arr[i])
i++
}
document.getElementById("list").innerHTML = JSON.stringify(list, null, 4)