forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (42 loc) · 815 Bytes
/
index.js
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
50
51
class Node {
constructor(data, next) {
this.data = data;
this.next = next;
}
}
class LinkedList {
constructor() {
this.head = null;
}
addAtBeginning(element) {
const node = new Node(element, null);
if (!this.head) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
addAtEnd(element) {
const node = new Node(element, null);
if (!this.head) {
this.head = node;
} else {
let address = this.head;
while (address.next) {
address = address.next;
}
address.next = node;
}
}
length() {
let address = this.head;
let count = 0;
while (address) {
count += 1;
address = address.next;
}
return count;
}
}
module.exports = { LinkedList, Node };