1var MyLinkedList = function () {
2 this.head = null;
3};
4
5
6
7
8
9MyLinkedList.prototype.get = function (index) {
10 let currentNode = this.head;
11 let currentIndex = 0;
12
13 while (currentNode) {
14 if (currentIndex === index) return currentNode.val;
15 currentNode = currentNode.next;
16 currentIndex++;
17 }
18
19 return -1;
20};
21
22
23
24
25
26MyLinkedList.prototype.addAtHead = function (val) {
27 const newHead = { val, next: this.head };
28 this.head = newHead;
29};
30
31
32
33
34
35MyLinkedList.prototype.addAtTail = function (val) {
36 if (!this.head) {
37 this.addAtHead(val);
38 return;
39 }
40 let currentNode = this.head;
41 while (currentNode.next) {
42 currentNode = currentNode.next;
43 }
44 currentNode.next = { val, next: null };
45};
46
47
48
49
50
51
52MyLinkedList.prototype.addAtIndex = function (index, val) {
53 let prevNode = null;
54 let currentNode = this.head;
55 let currentIndex = 0;
56 if (index === 0) {
57 this.addAtHead(val);
58 return;
59 }
60 while (currentNode) {
61 if (currentIndex === index) {
62 const newNode = { val, next: currentNode };
63 prevNode.next = newNode;
64 return;
65 }
66 prevNode = currentNode;
67 currentNode = currentNode.next;
68 currentIndex++;
69 }
70
71 if (currentIndex === index) {
72 const newNode = { val, next: null };
73 prevNode.next = newNode;
74 }
75};
76
77
78
79
80
81MyLinkedList.prototype.deleteAtIndex = function (index) {
82 let prevNode = null;
83 let currentNode = this.head;
84 let currentIndex = 0;
85 if (index === 0) {
86 this.head = this.head.next;
87 return;
88 }
89
90 while (currentNode) {
91 if (currentIndex === index) {
92 prevNode.next = currentNode.next;
93 }
94 prevNode = currentNode;
95 currentNode = currentNode.next;
96 currentIndex++;
97 }
98};
99
100
101
102
103
104
105
106
107
108
109function assert(expected, real, message = "") {
110 if (expected === real) {
111 console.log(`PASS: ${message}`);
112 } else {
113 console.log(`FAIL: ${message}`);
114 throw new Error(`FAIL: ${message}` + JSON.stringify({ expected, real }));
115 }
116}
117
118
119function test1() {
120 var obj = new MyLinkedList();
121 obj.addAtHead(1);
122 obj.addAtHead(2);
123 obj.addAtHead(3);
124
125 assert(obj.get(0), 3);
126 obj.deleteAtIndex(0);
127 assert(obj.get(0), 2);
128
129
130 obj.deleteAtIndex(1);
131
132 assert(obj.get(0), 2);
133 assert(obj.get(1), -1);
134}
135
136function test5() {
137 var obj = new MyLinkedList();
138 obj.addAtHead(7);
139 obj.addAtHead(2);
140 obj.addAtHead(1);
141
142 obj.addAtIndex(3, 0);
143
144 assert(0, obj.get(3));
145
146 obj.deleteAtIndex(2);
147
148 assert(0, obj.get(2));
149 obj.addAtHead(6);
150
151 assert(6, obj.get(0));
152
153 obj.addAtTail(4);
154
155 assert(4, obj.get(4));
156}
157
158function test6() {
159 var obj = new MyLinkedList();
160 obj.addAtIndex(0, 10);
161 obj.addAtIndex(0, 20);
162 obj.addAtIndex(1, 30);
163
164 assert(20, obj.get(0));
165}
166
167function test7() {
168 var obj = new MyLinkedList();
169 obj.addAtTail(1);
170 assert(1, obj.get(0));
171}
172test1();
173test5();
174test6();
175
176test7();
177