Skip to content

Commit 118dc56

Browse files
committed
feat: add solutions to lc problem: No.0707
No.0707.Design Linked List
1 parent 36476c8 commit 118dc56

File tree

8 files changed

+426
-223
lines changed

8 files changed

+426
-223
lines changed

solution/0300-0399/0320.Generalized Abbreviation/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li>例如,<code>"abcde"</code> 可以缩写为:
13-
1413
<ul>
1514
<li><code>"a3e"</code>(<code>"bcd"</code> 变为 <code>"3"</code> )</li>
1615
<li><code>"1bcd1"</code>(<code>"a"</code> 和 <code>"e"</code> 都变为 <code>"1"</code>)<meta charset="UTF-8" /></li>
@@ -24,7 +23,6 @@
2423
<li><meta charset="UTF-8" /><code>"22de"</code>&nbsp;(<code>"ab"</code> 变为&nbsp;<code>"2"</code>&nbsp;,&nbsp;<code>"bc"</code>&nbsp;变为&nbsp;<code>"2"</code>) &nbsp;是无效的,因为被选择的字符串是重叠的</li>
2524
</ul>
2625
</li>
27-
2826
</ul>
2927

3028
<p>给你一个字符串&nbsp;<code>word</code> ,返回&nbsp;<em>一个由</em>&nbsp;<code>word</code> 的<em>所有可能 <strong>广义缩写词</strong> 组成的列表</em>&nbsp;。按 <strong>任意顺序</strong> 返回答案。</p>

solution/0300-0399/0320.Generalized Abbreviation/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ul>
1010
<li>For example, <code>&quot;abcde&quot;</code> can be abbreviated into:
11-
1211
<ul>
1312
<li><code>&quot;a3e&quot;</code> (<code>&quot;bcd&quot;</code> turned into <code>&quot;3&quot;</code>)</li>
1413
<li><code>&quot;1bcd1&quot;</code> (<code>&quot;a&quot;</code> and <code>&quot;e&quot;</code> both turned into <code>&quot;1&quot;</code>)</li>
@@ -22,7 +21,6 @@
2221
<li><code>&quot;22de&quot;</code> (<code>&quot;ab&quot;</code> turned into <code>&quot;2&quot;</code> and <code>&quot;bc&quot;</code> turned into <code>&quot;2&quot;</code>) is invalid as the substring chosen overlap.</li>
2322
</ul>
2423
</li>
25-
2624
</ul>
2725

2826
<p>Given a string <code>word</code>, return <em>a list of all the possible <strong>generalized abbreviations</strong> of</em> <code>word</code>. Return the answer in <strong>any order</strong>.</p>

solution/0700-0799/0707.Design Linked List/README.md

+175-48
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,31 @@ linkedList.get(1); //返回3
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
**方法一:指针引用**
48+
**方法一:指针引用实现单链表**
4949

50-
定义虚拟头结点 dummy,count 记录当前链表结点个数。
50+
我们创建链表虚拟头结点 `dummy`,用变量 `cnt` 记录当前链表结点个数。
5151

52-
**方法二:数组**
52+
具体的方法如下:
5353

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)$。
5559

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` 存储链表节点的个数
6173

6274
<!-- tabs:start -->
6375

@@ -66,19 +78,14 @@ linkedList.get(1); //返回3
6678
<!-- 这里可写当前语言的特殊实现逻辑 -->
6779

6880
```python
69-
class ListNode:
70-
def __init__(self, val=0, next=None):
71-
self.val = val
72-
self.next = next
73-
74-
7581
class MyLinkedList:
82+
7683
def __init__(self):
7784
self.dummy = ListNode()
78-
self.count = 0
85+
self.cnt = 0
7986

8087
def get(self, index: int) -> int:
81-
if index < 0 or index >= self.count:
88+
if index < 0 or index >= self.cnt:
8289
return -1
8390
cur = self.dummy.next
8491
for _ in range(index):
@@ -89,27 +96,27 @@ class MyLinkedList:
8996
self.addAtIndex(0, val)
9097

9198
def addAtTail(self, val: int) -> None:
92-
self.addAtIndex(self.count, val)
99+
self.addAtIndex(self.cnt, val)
93100

94101
def addAtIndex(self, index: int, val: int) -> None:
95-
if index > self.count:
102+
if index > self.cnt:
96103
return
97104
pre = self.dummy
98105
for _ in range(index):
99106
pre = pre.next
100107
pre.next = ListNode(val, pre.next)
101-
self.count += 1
108+
self.cnt += 1
102109

103110
def deleteAtIndex(self, index: int) -> None:
104-
if index < 0 or index >= self.count:
111+
if index >= self.cnt:
105112
return
106113
pre = self.dummy
107114
for _ in range(index):
108115
pre = pre.next
109116
t = pre.next
110117
pre.next = t.next
111118
t.next = None
112-
self.count -= 1
119+
self.cnt -= 1
113120

114121

115122
# Your MyLinkedList object will be instantiated and called as such:
@@ -195,31 +202,18 @@ class MyLinkedList:
195202

196203
```java
197204
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;
212207

213208
public MyLinkedList() {
214-
dummy = new ListNode(0);
215-
count = 0;
209+
216210
}
217211

218212
public int get(int index) {
219-
if (index < 0 || index >= count) {
213+
if (index < 0 || index >= cnt) {
220214
return -1;
221215
}
222-
ListNode cur = dummy.next;
216+
var cur = dummy.next;
223217
while (index-- > 0) {
224218
cur = cur.next;
225219
}
@@ -231,33 +225,33 @@ class MyLinkedList {
231225
}
232226

233227
public void addAtTail(int val) {
234-
addAtIndex(count, val);
228+
addAtIndex(cnt, val);
235229
}
236230

237231
public void addAtIndex(int index, int val) {
238-
if (index > count) {
232+
if (index > cnt) {
239233
return;
240234
}
241-
ListNode pre = dummy;
235+
var pre = dummy;
242236
while (index-- > 0) {
243237
pre = pre.next;
244238
}
245239
pre.next = new ListNode(val, pre.next);
246-
++count;
240+
++cnt;
247241
}
248242

249243
public void deleteAtIndex(int index) {
250-
if (index < 0 || index >= count) {
244+
if (index < 0 || index >= cnt) {
251245
return;
252246
}
253-
ListNode pre = dummy;
247+
var pre = dummy;
254248
while (index-- > 0) {
255249
pre = pre.next;
256250
}
257-
ListNode t = pre.next;
251+
var t = pre.next;
258252
pre.next = t.next;
259253
t.next = null;
260-
--count;
254+
--cnt;
261255
}
262256
}
263257

@@ -350,6 +344,73 @@ class MyLinkedList {
350344

351345
### **C++**
352346

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+
353414
```cpp
354415
class MyLinkedList {
355416
public:
@@ -423,6 +484,72 @@ public:
423484
424485
### **Go**
425486
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+
426553
```go
427554
type MyLinkedList struct {
428555
e []int

0 commit comments

Comments
 (0)