@@ -225,6 +225,220 @@ class MyLinkedList {
225
225
*/
226
226
```
227
227
228
+ ### ** TypeScript**
229
+
230
+ ``` ts
231
+ class LinkNode {
232
+ public val: number
233
+ public next: LinkNode
234
+
235
+ constructor (val : number , next : LinkNode = null ) {
236
+ this .val = val ;
237
+ this .next = next ;
238
+ }
239
+ }
240
+
241
+ class MyLinkedList {
242
+ public head: LinkNode
243
+
244
+ constructor () {
245
+ this .head = null ;
246
+ }
247
+
248
+ get(index : number ): number {
249
+ if (this .head == null ) {
250
+ return - 1
251
+ }
252
+ let cur = this .head
253
+ let idxCur = 0 ;
254
+ while (idxCur < index ) {
255
+ if (cur .next == null ) {
256
+ return - 1
257
+ }
258
+ cur = cur .next ;
259
+ idxCur ++
260
+ }
261
+ return cur .val
262
+ }
263
+
264
+ addAtHead(val : number ): void {
265
+ this .head = new LinkNode (val , this .head );
266
+ }
267
+
268
+ addAtTail(val : number ): void {
269
+ const newNode = new LinkNode (val )
270
+ if (this .head == null ) {
271
+ this .head = newNode
272
+ return
273
+ }
274
+ let cur = this .head ;
275
+ while (cur .next != null ) {
276
+ cur = cur .next
277
+ }
278
+ cur .next = newNode ;
279
+ }
280
+
281
+ addAtIndex(index : number , val : number ): void {
282
+ if (index <= 0 ) {
283
+ return this .addAtHead (val );
284
+ }
285
+ const dummy = new LinkNode (0 , this .head );
286
+ let cur = dummy ;
287
+ let idxCur = 0 ;
288
+ while (idxCur < index ) {
289
+ if (cur .next == null ) {
290
+ return
291
+ }
292
+ cur = cur .next
293
+ idxCur ++
294
+ }
295
+ cur .next = new LinkNode (val , cur .next || null )
296
+ }
297
+
298
+ deleteAtIndex(index : number ): void {
299
+ if (index == 0 ) {
300
+ this .head = (this .head || {}).next
301
+ return
302
+ }
303
+ const dummy = new LinkNode (0 , this .head );
304
+ let cur = dummy ;
305
+ let idxCur = 0
306
+ while (idxCur < index ) {
307
+ if (cur .next == null ) {
308
+ return
309
+ }
310
+ cur = cur .next
311
+ idxCur ++
312
+ }
313
+ cur .next = (cur .next || {}).next
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Your MyLinkedList object will be instantiated and called as such:
319
+ * var obj = new MyLinkedList()
320
+ * var param_1 = obj.get(index)
321
+ * obj.addAtHead(val)
322
+ * obj.addAtTail(val)
323
+ * obj.addAtIndex(index,val)
324
+ * obj.deleteAtIndex(index)
325
+ */
326
+ ```
327
+
328
+ ### ** Rust**
329
+
330
+ ``` rust
331
+ struct Node {
332
+ val : i32 ,
333
+ next : Option <Box <Node >>,
334
+ }
335
+
336
+ #[derive(Default )]
337
+ struct MyLinkedList {
338
+ head : Option <Box <Node >>,
339
+ }
340
+
341
+ /**
342
+ * `&self` means the method takes an immutable reference.
343
+ * If you need a mutable reference, change it to `&mut self` instead.
344
+ */
345
+ impl MyLinkedList {
346
+ fn new () -> Self {
347
+ Default :: default ()
348
+ }
349
+
350
+ fn get (& self , index : i32 ) -> i32 {
351
+ let mut cur = match self . head {
352
+ None => return - 1 ,
353
+ Some (ref n ) => n ,
354
+ };
355
+ let mut idx_cur = 0 ;
356
+ while idx_cur < index {
357
+ match cur . next {
358
+ None => return - 1 ,
359
+ Some (ref next ) => {
360
+ cur = next ;
361
+ idx_cur += 1 ;
362
+ }
363
+ }
364
+ }
365
+ cur . val
366
+ }
367
+
368
+ fn add_at_head (& mut self , val : i32 ) {
369
+ self . head = Some (Box :: new (Node {
370
+ val ,
371
+ next : self . head. take (),
372
+ }));
373
+ }
374
+
375
+ fn add_at_tail (& mut self , val : i32 ) {
376
+ let new_node = Some (Box :: new (Node { val , next : None }));
377
+ let mut cur = match self . head {
378
+ Some (ref mut n ) => n ,
379
+ None => {
380
+ self . head = new_node ;
381
+ return ;
382
+ }
383
+ };
384
+ while let Some (ref mut next ) = cur . next {
385
+ cur = next ;
386
+ }
387
+ cur . next = new_node ;
388
+ }
389
+
390
+ fn add_at_index (& mut self , index : i32 , val : i32 ) {
391
+ let mut dummy = Box :: new (Node {
392
+ val : 0 ,
393
+ next : self . head. take ()
394
+ });
395
+ let mut idx = 0 ;
396
+ let mut cur = & mut dummy ;
397
+ while idx < index {
398
+ if let Some (ref mut next ) = cur . next {
399
+ cur = next ;
400
+ } else {
401
+ return
402
+ }
403
+ idx += 1 ;
404
+ }
405
+ cur . next = Some (Box :: new (Node {
406
+ val ,
407
+ next : cur . next. take ()
408
+ }));
409
+ self . head = dummy . next;
410
+ }
411
+
412
+ fn delete_at_index (& mut self , index : i32 ) {
413
+ let mut dummy = Box :: new (Node {
414
+ val : 0 ,
415
+ next : self . head. take (),
416
+ });
417
+ let mut idx = 0 ;
418
+ let mut cur = & mut dummy ;
419
+ while idx < index {
420
+ if let Some (ref mut next ) = cur . next {
421
+ cur = next ;
422
+ }
423
+ idx += 1 ;
424
+ }
425
+ cur . next = cur . next. take (). and_then (| n | n . next);
426
+ self . head = dummy . next;
427
+ }
428
+ }
429
+
430
+
431
+ /**
432
+ * Your MyLinkedList object will be instantiated and called as such:
433
+ * let obj = MyLinkedList::new();
434
+ * let ret_1: i32 = obj.get(index);
435
+ * obj.add_at_head(val);
436
+ * obj.add_at_tail(val);
437
+ * obj.add_at_index(index, val);
438
+ * obj.delete_at_index(index);
439
+ */
440
+ ```
441
+
228
442
### ** ...**
229
443
230
444
```
0 commit comments