707.Design Linked List
707.Design Linked List
难度:Easy
Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.
Implement these functions in your linked list class:
get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1. addAtHead(val) : Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. addAtTail(val) : Append a node of value val to the last element of the linked list. addAtIndex(index, val) : Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
1
Example:
2
3
MyLinkedList linkedList = new MyLinkedList();
4
linkedList.addAtHead(1);
5
linkedList.addAtTail(3);
6
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
7
linkedList.get(1); // returns 2
8
linkedList.deleteAtIndex(1); // now the linked list is 1->3
9
linkedList.get(1); // returns 3
10
Note:
11
12
All values will be in the range of [1, 1000].
13
The number of operations will be in the range of [1, 1000].
14
Please do not use the built-in LinkedList library.
Copied!
需要注意增加删除节点时的逻辑性,如在某个节点后添加节点,需要先处理完这个节点之后的节点,才能将cur->next=newNode,同理删除节点也是一样。
1
class List{
2
public:
3
int val;
4
List* next;
5
List* pre;
6
List(int v, List* n, List* p ):val(v),next(n),pre(p){}
7
};
8
class MyLinkedList {
9
public:
10
List* root;
11
List* trail;
12
int size;
13
/** Initialize your data structure here. */
14
MyLinkedList() {
15
root=nullptr;
16
trail = nullptr;
17
size=0;
18
}
19
20
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
21
int get(int index) {
22
if(index<0 || index>=size) return -1;
23
List* cur=root;
24
// cout<<index<< " "<<size<<endl;
25
while(index--)
26
{
27
cur=cur->next;
28
}
29
return cur->val;
30
}
31
32
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
33
void addAtHead(int val) {
34
if(size==0)
35
{
36
root = new List(val, nullptr,nullptr);
37
trail=root;
38
}
39
else
40
{
41
List* newNode = new List(val, root,nullptr);
42
root->pre= newNode;
43
root=newNode;
44
}
45
size++;
46
}
47
48
/** Append a node of value val to the last element of the linked list. */
49
void addAtTail(int val) {
50
if(size==0)
51
{
52
addAtHead(val);
53
return;
54
}
55
56
List *newNode = new List(val, nullptr, trail);
57
trail->next = newNode;
58
trail = newNode;
59
size++;
60
}
61
62
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
63
void addAtIndex(int index, int val) {
64
// cout<<size<<endl;
65
66
if(index > size) return;
67
68
if(index<=0)
69
{
70
addAtHead(val);
71
return;
72
}
73
if(index == size) {
74
addAtTail(val);
75
return;
76
}
77
List* cur = root;
78
while(--index)
79
{
80
cur = cur->next;
81
}
82
// cout<<cur->val<<endl;
83
List* newNode = new List(val, cur->next, cur);
84
cur->next->pre = newNode;
85
cur->next=newNode;
86
87
size++;
88
}
89
90
/** Delete the index-th node in the linked list, if the index is valid. */
91
void deleteAtIndex(int index) {
92
// cout<<size<<" "<<index<<endl;
93
if(index <0 || size==0 || index>size-1) return;
94
if(size ==1 )
95
{
96
List* tmp=root;
97
root=nullptr;
98
trail=nullptr;
99
delete tmp;
100
size--;
101
return;
102
}
103
if(index ==0)
104
{
105
List* old=root;
106
root=root->next;
107
root->pre=nullptr;
108
size--;
109
delete old;
110
return;
111
}
112
if(index == size -1)
113
{
114
List* old=trail;
115
trail = trail->pre;
116
trail->next =nullptr;
117
size--;
118
delete old;
119
return;
120
}
121
List* cur=root;
122
while(--index)
123
{
124
cur=cur->next;
125
}
126
127
List* tmp=cur->next;
128
cur->next->next->pre= cur;
129
cur->next = cur->next->next;
130
131
delete tmp;
132
size--;
133
134
}
135
};
136
137
/**
138
* Your MyLinkedList object will be instantiated and called as such:
139
* MyLinkedList* obj = new MyLinkedList();
140
* int param_1 = obj->get(index);
141
* obj->addAtHead(val);
142
* obj->addAtTail(val);
143
* obj->addAtIndex(index,val);
144
* obj->deleteAtIndex(index);
145
**/
Copied!
执行用时 :96 ms, 在所有 C++ 提交中击败了34.36%的用户 内存消耗 :19.5 MB, 在所有 C++ 提交中击败了35.99%的用户
Copy link