-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_1_linked-list.js
More file actions
49 lines (38 loc) · 921 Bytes
/
Copy path04_1_linked-list.js
File metadata and controls
49 lines (38 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { performance } = require('perf_hooks');
const startingTime = performance.now();
// Start of code
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
}
class Node {
constructor(value, next, prev) {
this.value = value;
this.next = next;
this.prev = prev;
}
}
LinkedList.prototype.addHead = function(value) {
// Next should be current head
let newNode = new Node(value, this.head, null);
// if we have a head
if (this.head) {
//console.log(this.head)
// we set this new node as prev
this.head.prev = newNode;
//this.head = newNode;
} else {
this.tail = newNode;
}
// New head of the List
this.head = newNode;
};
let LL = new LinkedList();
LL.addHead(10);
LL.addHead(20);
console.log(LL);
// End of code
const endingTime = performance.now();
console.log('Function took ' + (endingTime - startingTime) + ' milliseconds.');