@@ -50,13 +50,15 @@ myLinkedList.get(1); // return 3
50
50
- At most ` 2000 ` calls will be made to ` get ` , ` addAtHead ` , ` addAtTail ` , ` addAtIndex ` and ` deleteAtIndex ` .
51
51
52
52
## Intuition behind the Solution
53
+ This question can comprehensively test the candidate's mastery of linked lists. The following points need to be paid attention to:
53
54
54
- ## Steps to the Solution
55
-
55
+ 1 . It is best to use a ` dummyHead ` node as the entry of the linked list.
56
+ 2 . It is best to use a new ` LinkedNode ` class, so that ` dummyHead ` does not need to be mixed with ` val ` and ` next ` .
57
+ 3 . Implement the easy methods first, in the order of ` addAtHead ` , ` addAtTail ` , ` addAtIndex ` , ` deleteAtIndex ` , ` get ` .
56
58
57
59
## Complexity
58
- * Time: ` O() ` .
59
- * Space: ` O() ` .
60
+ * Time: ` O(n * n ) ` .
61
+ * Space: ` O(n ) ` .
60
62
61
63
## Java
62
64
``` java
@@ -208,12 +210,166 @@ class MyLinkedList:
208
210
209
211
## JavaScript
210
212
``` javascript
211
- // Welcome to create a PR to complete the code of this language, thanks!
213
+ class LinkedNode {
214
+ constructor (val ) {
215
+ this .val = val
216
+ this .next = null
217
+ }
218
+ }
219
+
220
+ var MyLinkedList = function () {
221
+ this .dummyHead = new LinkedNode (0 )
222
+ };
223
+
224
+ MyLinkedList .prototype .get = function (index ) {
225
+ let node = this .dummyHead .next
226
+ let i = 0
227
+
228
+ while (node != null && i < index) {
229
+ node = node .next
230
+ i += 1
231
+ }
232
+
233
+ if (i == index && node != null ) {
234
+ return node .val
235
+ }
236
+
237
+ return - 1
238
+ };
239
+
240
+ MyLinkedList .prototype .addAtHead = function (val ) {
241
+ const node = new LinkedNode (val)
242
+ node .next = this .dummyHead .next
243
+ this .dummyHead .next = node
244
+ };
245
+
246
+ MyLinkedList .prototype .addAtTail = function (val ) {
247
+ let node = this .dummyHead
248
+
249
+ while (node .next != null ) {
250
+ node = node .next
251
+ }
252
+
253
+ node .next = new LinkedNode (val)
254
+ };
255
+
256
+ MyLinkedList .prototype .addAtIndex = function (index , val ) {
257
+ let node = this .dummyHead
258
+ let i = 0
259
+
260
+ while (node .next != null && i < index) {
261
+ node = node .next
262
+ i += 1
263
+ }
264
+
265
+ if (i == index) {
266
+ const newNode = new LinkedNode (val);
267
+ newNode .next = node .next ;
268
+ node .next = newNode;
269
+ }
270
+ };
271
+
272
+ MyLinkedList .prototype .deleteAtIndex = function (index ) {
273
+ let node = this .dummyHead
274
+ let i = 0
275
+
276
+ while (node .next != null && i < index) {
277
+ node = node .next
278
+ i += 1
279
+ }
280
+
281
+ if (i == index && node .next != null ) {
282
+ node .next = node .next .next
283
+ }
284
+ };
212
285
```
213
286
214
287
## C#
215
288
``` c#
216
- // Welcome to create a PR to complete the code of this language, thanks!
289
+ public class LinkedNode
290
+ {
291
+ public int val ;
292
+ public LinkedNode next ;
293
+
294
+ public LinkedNode (int val )
295
+ {
296
+ this .val = val ;
297
+ }
298
+ }
299
+
300
+ public class MyLinkedList
301
+ {
302
+ LinkedNode dummyHead = new LinkedNode (0 );
303
+
304
+ public MyLinkedList () {}
305
+
306
+ public int Get (int index )
307
+ {
308
+ var node = dummyHead .next ;
309
+ int i = 0 ;
310
+
311
+ while (node != null && i < index )
312
+ {
313
+ node = node .next ;
314
+ i += 1 ;
315
+ }
316
+
317
+ if (i == index && node != null )
318
+ return node .val ;
319
+
320
+ return - 1 ;
321
+ }
322
+
323
+ public void AddAtHead (int val )
324
+ {
325
+ var node = new LinkedNode (val );
326
+ node .next = dummyHead .next ;
327
+ dummyHead .next = node ;
328
+ }
329
+
330
+ public void AddAtTail (int val )
331
+ {
332
+ var node = dummyHead ;
333
+
334
+ while (node .next != null )
335
+ node = node .next ;
336
+
337
+ node .next = new LinkedNode (val );
338
+ }
339
+
340
+ public void AddAtIndex (int index , int val )
341
+ {
342
+ var node = dummyHead ;
343
+ int i = 0 ;
344
+
345
+ while (node .next != null && i < index )
346
+ {
347
+ node = node .next ;
348
+ i += 1 ;
349
+ }
350
+
351
+ if (i == index ) {
352
+ var newNode = new LinkedNode (val );
353
+ newNode .next = node .next ;
354
+ node .next = newNode ;
355
+ }
356
+ }
357
+
358
+ public void DeleteAtIndex (int index )
359
+ {
360
+ var node = dummyHead ;
361
+ int i = 0 ;
362
+
363
+ while (node .next != null && i < index )
364
+ {
365
+ node = node .next ;
366
+ i += 1 ;
367
+ }
368
+
369
+ if (i == index && node .next != null )
370
+ node .next = node .next .next ;
371
+ }
372
+ }
217
373
```
218
374
219
375
## Go
@@ -251,15 +407,9 @@ class MyLinkedList:
251
407
// Welcome to create a PR to complete the code of this language, thanks!
252
408
```
253
409
254
- ## 问题描述
255
-
256
-
257
- ### [ Example 1]
258
- ![ ] ( ../../images/examples/- )
259
-
260
- ** Input** : ``
261
-
262
- ** Output** : ``
263
-
264
410
## 中文题解
411
+ 本题可以全面考察候选人对链表的掌握程度,以下几点需要重视:
265
412
413
+ 1 . 最好使用一个` dummyHead ` 节点做为链表入口。
414
+ 2 . 最好使用一个新的` LinkedNode ` 类,这样,` dummyHead ` 就不用和` val ` 、` next ` 混在一起。
415
+ 3 . 先实现容易的方法,顺序为` addAtHead ` , ` addAtTail ` , ` addAtIndex ` , ` deleteAtIndex ` , ` get ` 。
0 commit comments