Skip to content

Commit 8b0d5b2

Browse files
Merge pull request youngyangyang04#2639 from Relsola/master
Update 707.设计链表 优化 C 代码结构和逻辑
2 parents c86999e + 22a6522 commit 8b0d5b2

File tree

1 file changed

+65
-70
lines changed

1 file changed

+65
-70
lines changed

problems/0707.设计链表.md

Lines changed: 65 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -168,109 +168,103 @@ private:
168168
### C:
169169
170170
```C
171-
typedef struct MyLinkedList {
172-
int val;
173-
struct MyLinkedList* next;
174-
}MyLinkedList;
171+
typedef struct Node {
172+
int val;
173+
struct Node* next;
174+
} Node;
175+
176+
177+
typedef struct {
178+
int size;
179+
Node* data;
180+
} MyLinkedList;
175181
176182
/** Initialize your data structure here. */
177183
178184
MyLinkedList* myLinkedListCreate() {
179-
//这个题必须用虚拟头指针,参数都是一级指针,头节点确定后没法改指向了!!!
180-
MyLinkedList* head = (MyLinkedList *)malloc(sizeof (MyLinkedList));
181-
head->next = NULL;
182-
return head;
185+
MyLinkedList* obj = (MyLinkedList*)malloc(sizeof(MyLinkedList));
186+
Node* head = (Node*)malloc(sizeof(Node));
187+
head->next = (void*)0;
188+
obj->data = head;
189+
obj->size = 0;
190+
return obj;
183191
}
184192
185193
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
186194
int myLinkedListGet(MyLinkedList* obj, int index) {
187-
MyLinkedList *cur = obj->next;
188-
for (int i = 0; cur != NULL; i++){
189-
if (i == index){
190-
return cur->val;
191-
}
192-
else{
193-
cur = cur->next;
194-
}
195+
if (index < 0 || index >= obj->size) return -1;
196+
197+
Node* cur = obj->data;
198+
while (index-- >= 0) {
199+
cur = cur->next;
195200
}
196-
return -1;
201+
202+
return cur->val;
197203
}
198204
199205
/** 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. */
200206
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
201-
MyLinkedList *nhead = (MyLinkedList *)malloc(sizeof (MyLinkedList));
202-
nhead->val = val;
203-
nhead->next = obj->next;
204-
obj->next = nhead;
207+
Node* node = (Node*)malloc(sizeof(Node));
208+
node->val = val;
205209
210+
node->next = obj->data->next;
211+
obj->data->next = node;
212+
obj->size++;
206213
}
207214
208215
/** Append a node of value val to the last element of the linked list. */
209216
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
210-
MyLinkedList *cur = obj;
211-
while(cur->next != NULL){
217+
Node* cur = obj->data;
218+
while (cur->next != ((void*)0)) {
212219
cur = cur->next;
213220
}
214-
MyLinkedList *ntail = (MyLinkedList *)malloc(sizeof (MyLinkedList));
215-
ntail->val = val;
216-
ntail->next = NULL;
217-
cur->next = ntail;
221+
222+
Node* tail = (Node*)malloc(sizeof(Node));
223+
tail->val = val;
224+
tail->next = (void*)0;
225+
cur->next = tail;
226+
obj->size++;
218227
}
219228
220229
/** 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. */
221230
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
222-
if (index == 0){
223-
myLinkedListAddAtHead(obj, val);
224-
return;
225-
}
226-
MyLinkedList *cur = obj->next;
227-
for (int i = 1 ;cur != NULL; i++){
228-
if (i == index){
229-
MyLinkedList* newnode = (MyLinkedList *)malloc(sizeof (MyLinkedList));
230-
newnode->val = val;
231-
newnode->next = cur->next;
232-
cur->next = newnode;
233-
return;
234-
}
235-
else{
236-
cur = cur->next;
237-
}
231+
if (index > obj->size) return;
232+
233+
Node* cur = obj->data;
234+
while (index-- > 0) {
235+
cur = cur->next;
238236
}
237+
238+
Node* node = (Node*)malloc(sizeof(Node));
239+
node->val = val;
240+
node->next = cur->next;
241+
cur->next = node;
242+
obj->size++;
239243
}
240244
241245
/** Delete the index-th node in the linked list, if the index is valid. */
242246
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
243-
if (index == 0){
244-
MyLinkedList *tmp = obj->next;
245-
if (tmp != NULL){
246-
obj->next = tmp->next;
247-
free(tmp);
248-
}
249-
return;
250-
}
251-
MyLinkedList *cur = obj->next;
252-
for (int i = 1 ;cur != NULL && cur->next != NULL; i++){
253-
if (i == index){
254-
MyLinkedList *tmp = cur->next;
255-
if (tmp != NULL) {
256-
cur->next = tmp->next;
257-
free(tmp);
258-
}
259-
return;
260-
}
261-
else{
262-
cur = cur->next;
263-
}
247+
if (index < 0 || index >= obj->size) return;
248+
249+
Node* cur = obj->data;
250+
while (index-- > 0) {
251+
cur = cur->next;
264252
}
265-
253+
254+
Node* temp = cur->next;
255+
cur->next = temp->next;
256+
free(temp);
257+
obj->size--;
266258
}
267259
268260
void myLinkedListFree(MyLinkedList* obj) {
269-
while(obj != NULL){
270-
MyLinkedList *tmp = obj;
271-
obj = obj->next;
272-
free(tmp);
273-
}
261+
Node* tmp = obj->data;
262+
while (tmp != NULL) {
263+
Node* n = tmp;
264+
tmp = tmp->next;
265+
free(n);
266+
}
267+
free(obj);
274268
}
275269
276270
/**
@@ -1569,3 +1563,4 @@ public class MyLinkedList
15691563
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
15701564
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
15711565
</a>
1566+

0 commit comments

Comments
 (0)