-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (37 loc) · 1.01 KB
/
index.js
File metadata and controls
52 lines (37 loc) · 1.01 KB
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
52
class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
class LinkedList {
constructor() {
this.head = null;
}
}
const list = new LinkedList();
LinkedList.prototype.insertAtEnd = function(data) {
let newNode = new Node(data);
if (this.head === null) {
this.head = newNode;
return this.head;
}
// else
let tail = this.head;
while (tail.next !== null) {
tail = tail.next;
}
tail.next = newNode;
return this.head;
};
LinkedList.prototype.insertAtBeginning = function(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
return this.head;
};
list.insertAtBeginning({ name: "bob", lastName: "harry" });
list.insertAtBeginning({ name: "rick", lastName: "barry" });
list.insertAtBeginning({ name: "john", lastName: "scudiery" });
list.insertAtEnd({ name: "bill", lastName: "norington" });
console.log(list);