-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
70 lines (64 loc) · 1.57 KB
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// OJ: https://leetcode.com/problems/design-linked-list/
// Author: github.com/lzl124631x
// Time:
// get: O(N)
// addAtHead: O(1)
// addAtTail: O(1)
// addAtIndex: O(N)
// deleteAtIndex: O(N)
// Space: O(1)
class MyListNode {
public:
int val;
MyListNode *next = NULL;
MyListNode(int v): val(v) {}
};
class MyLinkedList {
private:
MyListNode dummy = MyListNode(0), *tail = &dummy;
int len = 0;
public:
MyLinkedList() {}
int get(int index) {
if (index >= len) return -1;
auto p = dummy.next;
while (index--) p = p->next;
return p->val;
}
void addAtHead(int val) {
auto node = new MyListNode(val);
node->next = dummy.next;
dummy.next = node;
if (tail == &dummy) tail = node;
++len;
}
void addAtTail(int val) {
tail->next = new MyListNode(val);
tail = tail->next;
++len;
}
void addAtIndex(int index, int val) {
if (index > len) return;
auto node = new MyListNode(val);
if (index == len) {
tail->next = node;
tail = node;
} else {
auto p = &dummy;
while (index--) p = p->next;
node->next = p->next;
p->next = node;
}
++len;
}
void deleteAtIndex(int index) {
if (index >= len) return;
auto p = &dummy;
while (index--) p = p->next;
auto node = p->next;
p->next = node->next;
if (tail == node) tail = p;
delete node;
--len;
}
};