Design Linked List LeetCode solution in JavaScript

1
var MyLinkedList = function () {
2
this.linked = [];
3
};
4

5
MyLinkedList.prototype.get = function (index) {
6
if (index < this.linked.length) {
7
return this.linked[index];
8
}
9
return -1;
10
};
11

12
MyLinkedList.prototype.addAtHead = function (val) {
13
this.linked.unshift(val);
14
};
15

16
MyLinkedList.prototype.addAtTail = function (val) {
17
this.linked.push(val);
18
};
19

20
MyLinkedList.prototype.addAtIndex = function (index, val) {
21
if (index <= this.linked.length) {
22
this.linked.splice(index, 0, val);
23
}
24
};
25

26
MyLinkedList.prototype.deleteAtIndex = function (index) {
27
this.linked.splice(index, 1);
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0