@@ -45,19 +45,31 @@ linkedList.get(1); //返回3
45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
- ** 方法一:指针引用 **
48
+ ** 方法一:指针引用实现单链表 **
49
49
50
- 定义虚拟头结点 dummy,count 记录当前链表结点个数。
50
+ 我们创建链表虚拟头结点 ` dummy ` ,用变量 ` cnt ` 记录当前链表结点个数。
51
51
52
- ** 方法二:数组 **
52
+ 具体的方法如下:
53
53
54
- 数组模拟单链表,其中:
54
+ - ` get(index) ` :遍历链表,找到第 ` index ` 个结点,返回其值,如果不存在,返回 ` -1 ` 。时间复杂度 $O(n)$。
55
+ - ` addAtHead(val) ` :创建新结点,将其插入到虚拟头结点后面。时间复杂度 $O(1)$。
56
+ - ` addAtTail(val) ` :创建新结点,将其插入到链表尾部。时间复杂度 $O(n)$。
57
+ - ` addAtIndex(index, val) ` :如果 ` index ` 等于链表长度,则该节点将附加到链表的末尾。如果 ` index ` 大于链表长度,则不会插入节点。如果 ` index ` 小于 0,则在头部插入节点。否则,遍历链表,找到第 ` index ` 个结点的前一个结点,将新结点插入到该结点后面。时间复杂度 $O(n)$。
58
+ - ` deleteAtIndex(index) ` :如果索引 ` index ` 有效,则删除链表中的第 ` index ` 个节点。否则,不做任何操作。时间复杂度 $O(n)$。
55
59
56
- - head 存放链表头
57
- - e 存储链表节点的值
58
- - ne 存储链表节点的 next 指针
59
- - idx 指向当前可分配的节点下标
60
- - size 存储链表节点的个数
60
+ 时间复杂度见具体的方法说明。其中 $n$ 为链表长度。
61
+
62
+ 注意:LeetCode 平台已经内置 ListNode 单链表结点类,可以直接使用。
63
+
64
+ ** 方法二:静态数组实现单链表**
65
+
66
+ 我们也可以用静态数组来实现单链表,其中:
67
+
68
+ - ` head ` 存放链表头
69
+ - ` e ` 存储链表节点的值
70
+ - ` ne ` 存储链表节点的 ` next ` 指针
71
+ - ` idx ` 指向当前可分配的节点下标
72
+ - ` size ` 存储链表节点的个数
61
73
62
74
<!-- tabs:start -->
63
75
@@ -66,19 +78,14 @@ linkedList.get(1); //返回3
66
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
79
68
80
``` python
69
- class ListNode :
70
- def __init__ (self , val = 0 , next = None ):
71
- self .val = val
72
- self .next = next
73
-
74
-
75
81
class MyLinkedList :
82
+
76
83
def __init__ (self ):
77
84
self .dummy = ListNode()
78
- self .count = 0
85
+ self .cnt = 0
79
86
80
87
def get (self , index : int ) -> int :
81
- if index < 0 or index >= self .count :
88
+ if index < 0 or index >= self .cnt :
82
89
return - 1
83
90
cur = self .dummy.next
84
91
for _ in range (index):
@@ -89,27 +96,27 @@ class MyLinkedList:
89
96
self .addAtIndex(0 , val)
90
97
91
98
def addAtTail (self , val : int ) -> None :
92
- self .addAtIndex(self .count , val)
99
+ self .addAtIndex(self .cnt , val)
93
100
94
101
def addAtIndex (self , index : int , val : int ) -> None :
95
- if index > self .count :
102
+ if index > self .cnt :
96
103
return
97
104
pre = self .dummy
98
105
for _ in range (index):
99
106
pre = pre.next
100
107
pre.next = ListNode(val, pre.next)
101
- self .count += 1
108
+ self .cnt += 1
102
109
103
110
def deleteAtIndex (self , index : int ) -> None :
104
- if index < 0 or index >= self .count :
111
+ if index >= self .cnt :
105
112
return
106
113
pre = self .dummy
107
114
for _ in range (index):
108
115
pre = pre.next
109
116
t = pre.next
110
117
pre.next = t.next
111
118
t.next = None
112
- self .count -= 1
119
+ self .cnt -= 1
113
120
114
121
115
122
# Your MyLinkedList object will be instantiated and called as such:
@@ -195,31 +202,18 @@ class MyLinkedList:
195
202
196
203
``` java
197
204
class MyLinkedList {
198
- private class ListNode {
199
- int val;
200
- ListNode next;
201
- ListNode (int val ) {
202
- this (val, null );
203
- }
204
- ListNode (int val , ListNode next ) {
205
- this . val = val;
206
- this . next = next;
207
- }
208
- }
209
-
210
- private ListNode dummy;
211
- private int count;
205
+ private ListNode dummy = new ListNode ();
206
+ private int cnt;
212
207
213
208
public MyLinkedList () {
214
- dummy = new ListNode (0 );
215
- count = 0 ;
209
+
216
210
}
217
211
218
212
public int get (int index ) {
219
- if (index < 0 || index >= count ) {
213
+ if (index < 0 || index >= cnt ) {
220
214
return - 1 ;
221
215
}
222
- ListNode cur = dummy. next;
216
+ var cur = dummy. next;
223
217
while (index-- > 0 ) {
224
218
cur = cur. next;
225
219
}
@@ -231,33 +225,33 @@ class MyLinkedList {
231
225
}
232
226
233
227
public void addAtTail (int val ) {
234
- addAtIndex(count , val);
228
+ addAtIndex(cnt , val);
235
229
}
236
230
237
231
public void addAtIndex (int index , int val ) {
238
- if (index > count ) {
232
+ if (index > cnt ) {
239
233
return ;
240
234
}
241
- ListNode pre = dummy;
235
+ var pre = dummy;
242
236
while (index-- > 0 ) {
243
237
pre = pre. next;
244
238
}
245
239
pre. next = new ListNode (val, pre. next);
246
- ++ count ;
240
+ ++ cnt ;
247
241
}
248
242
249
243
public void deleteAtIndex (int index ) {
250
- if (index < 0 || index >= count ) {
244
+ if (index < 0 || index >= cnt ) {
251
245
return ;
252
246
}
253
- ListNode pre = dummy;
247
+ var pre = dummy;
254
248
while (index-- > 0 ) {
255
249
pre = pre. next;
256
250
}
257
- ListNode t = pre. next;
251
+ var t = pre. next;
258
252
pre. next = t. next;
259
253
t. next = null ;
260
- -- count ;
254
+ -- cnt ;
261
255
}
262
256
}
263
257
@@ -350,6 +344,73 @@ class MyLinkedList {
350
344
351
345
### ** C++**
352
346
347
+ ``` cpp
348
+ class MyLinkedList {
349
+ private:
350
+ ListNode* dummy = new ListNode();
351
+ int cnt = 0;
352
+
353
+ public:
354
+ MyLinkedList() {
355
+ }
356
+
357
+ int get(int index) {
358
+ if (index < 0 || index >= cnt) {
359
+ return -1;
360
+ }
361
+ auto cur = dummy->next;
362
+ while (index--) {
363
+ cur = cur->next;
364
+ }
365
+ return cur->val;
366
+ }
367
+
368
+ void addAtHead(int val) {
369
+ addAtIndex (0, val);
370
+ }
371
+
372
+ void addAtTail(int val) {
373
+ addAtIndex (cnt, val);
374
+ }
375
+
376
+ void addAtIndex(int index, int val) {
377
+ if (index < 0 || index > cnt) {
378
+ return;
379
+ }
380
+ auto pre = dummy;
381
+ while (index--) {
382
+ pre = pre->next;
383
+ }
384
+ pre->next = new ListNode(val, pre->next);
385
+ ++cnt;
386
+ }
387
+
388
+ void deleteAtIndex(int index) {
389
+ if (index >= cnt) {
390
+ return;
391
+ }
392
+ auto pre = dummy;
393
+ while (index-- > 0) {
394
+ pre = pre->next;
395
+ }
396
+ auto t = pre->next;
397
+ pre->next = t->next;
398
+ t->next = nullptr;
399
+ --cnt;
400
+ }
401
+ };
402
+
403
+ /* *
404
+ * Your MyLinkedList object will be instantiated and called as such:
405
+ * MyLinkedList* obj = new MyLinkedList();
406
+ * int param_1 = obj->get(index);
407
+ * obj->addAtHead(val);
408
+ * obj->addAtTail(val);
409
+ * obj->addAtIndex(index,val);
410
+ * obj->deleteAtIndex(index);
411
+ */
412
+ ```
413
+
353
414
``` cpp
354
415
class MyLinkedList {
355
416
public:
@@ -423,6 +484,72 @@ public:
423
484
424
485
### **Go**
425
486
487
+ ```go
488
+ type MyLinkedList struct {
489
+ dummy *ListNode
490
+ cnt int
491
+ }
492
+
493
+ func Constructor() MyLinkedList {
494
+ return MyLinkedList{&ListNode{}, 0}
495
+ }
496
+
497
+ func (this *MyLinkedList) Get(index int) int {
498
+ if index < 0 || index >= this.cnt {
499
+ return -1
500
+ }
501
+ cur := this.dummy.Next
502
+ for ; index > 0; index-- {
503
+ cur = cur.Next
504
+ }
505
+ return cur.Val
506
+ }
507
+
508
+ func (this *MyLinkedList) AddAtHead(val int) {
509
+ this.AddAtIndex(0, val)
510
+ }
511
+
512
+ func (this *MyLinkedList) AddAtTail(val int) {
513
+ this.AddAtIndex(this.cnt, val)
514
+ }
515
+
516
+ func (this *MyLinkedList) AddAtIndex(index int, val int) {
517
+ if index > this.cnt {
518
+ return
519
+ }
520
+ pre := this.dummy
521
+ for ; index > 0; index-- {
522
+ pre = pre.Next
523
+ }
524
+ pre.Next = &ListNode{val, pre.Next}
525
+ this.cnt++
526
+ }
527
+
528
+ func (this *MyLinkedList) DeleteAtIndex(index int) {
529
+ if index < 0 || index >= this.cnt {
530
+ return
531
+ }
532
+ pre := this.dummy
533
+ for ; index > 0; index-- {
534
+ pre = pre.Next
535
+ }
536
+ t := pre.Next
537
+ pre.Next = t.Next
538
+ t.Next = nil
539
+ this.cnt--
540
+ }
541
+
542
+ /**
543
+ * Your MyLinkedList object will be instantiated and called as such:
544
+ * obj := Constructor();
545
+ * param_1 := obj.Get(index);
546
+ * obj.AddAtHead(val);
547
+ * obj.AddAtTail(val);
548
+ * obj.AddAtIndex(index,val);
549
+ * obj.DeleteAtIndex(index);
550
+ */
551
+ ```
552
+
426
553
``` go
427
554
type MyLinkedList struct {
428
555
e []int
0 commit comments