void deleteAtIndex(int index) { if(index < 0){ return; } int k = 0; listNode* nowNode = virtNode; while(index--){ if(nowNode != NULL && nowNode->next != NULL){ nowNode = nowNode->next; } else{ return; } k++; } listNode* tmp = nowNode->next; if(nowNode!=NULL) nowNode->next = nowNode->next->next; delete tmp; //delete命令指示释放了tmp指针原本所指的那部分内存, //被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后, //如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针 //如果之后的程序不小心使用了tmp,会指向难以预想的内存空间 tmp=nullptr; }
时间: 2024-02-14 19:11:01 浏览: 23
这段代码看起来没有明显的错误,不过有一些可以改进的地方:
1. 在删除节点之前,应该先判断要删除的节点是否存在,否则可能会访问到空指针,导致程序崩溃。
2. 在删除节点之后,应该将指向该节点的指针设置为nullptr,以避免成为野指针。
改进后的代码如下所示:
```
void deleteAtIndex(int index) {
if (index < 0) {
return;
}
int k = 0;
listNode* nowNode = virtNode;
while (index-- > 0) {
if (nowNode != nullptr && nowNode->next != nullptr) {
nowNode = nowNode->next;
}
else {
return;
}
k++;
}
if (nowNode->next != nullptr) {
listNode* tmp = nowNode->next;
nowNode->next = nowNode->next->next;
delete tmp;
tmp = nullptr;
}
}
```
这样修改后,代码更加健壮,可以更好地处理边界情况。
相关问题
帮我看一下这段代码有什么问题ListNode* reverseKGroup(ListNode* head, int k) { if(head == NULL) return head; ListNode* a = head,* b = head; ListNode* pre = nullptr,* cur = head,* nxt = head; for(int i = 0; i++; i < k) { if(b == NULL) return head; b = b -> next; } while(cur != b) { nxt = cur -> next; cur -> next = pre; pre = cur; cur = nxt; } head -> next = reverseKGroup(b,k); return pre; }
这段代码的主要问题是:
1.循环条件错误,应为 i < k,而不是 i ;
2.代码中没有处理最后一组节点数量不足 k 的情况,应该在最后加上一个判断,如果剩余节点数量小于 k,则不需要翻转,直接返回原链表。
3.没有释放内存的操作,容易造成内存泄漏。
修改后的代码如下:
ListNode* reverseKGroup(ListNode* head, int k) {
if(head == NULL || k == 1) return head; // 链表为空或k为1时无需翻转
ListNode* a = head,* b = head;
ListNode* pre = nullptr,* cur = head,* nxt = head;
int count = 0;
while(b != NULL && count < k) { // 循环条件修改
b = b -> next;
count++;
}
if(count < k) return head; // 最后一组节点数量小于k,不需要翻转
while(cur != b) {
nxt = cur -> next;
cur -> next = pre;
pre = cur;
cur = nxt;
}
head -> next = reverseKGroup(b,k);
// 释放内存,避免内存泄漏
if(a != pre) a -> next = cur;
else a -> next = b;
return pre;
}
用C语言改写,将打印输出的结果中重复的数去掉#include <stdio.h>#include <stdlib.h>struct ListNode { int val; struct ListNode *next;};struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){ struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点 dummy->val = 0; dummy->next = NULL; struct ListNode* cur = dummy; while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } if (l1) cur->next = l1; // 将剩余的节点加入到新链表中 if (l2) cur->next = l2; return dummy->next;}int main() { int n, m, num; scanf("%d%d", &n, &m); struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* p1 = l1; struct ListNode* p2 = l2; for (int i = 0; i < n; i++) { scanf("%d", &num); p1->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p1->next->val = num; p1->next->next = NULL; p1 = p1->next; } for (int i = 0; i < m; i++) { scanf("%d", &num); p2->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p2->next->val = num; p2->next->next = NULL; p2 = p2->next; } struct ListNode* res = mergeList(l1->next, l2->next); while (res) { printf("%d ", res->val); res = res->next; } return 0;}
以下是用 C 语言改写,并去掉重复数的代码:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点
dummy->val = 0;
dummy->next = NULL;
struct ListNode* cur = dummy;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) cur->next = l1; // 将剩余的节点加入到新链表中
if (l2) cur->next = l2;
return dummy->next;
}
int main() {
int n, m, num;
scanf("%d%d", &n, &m);
struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* p1 = l1;
struct ListNode* p2 = l2;
for (int i = 0; i < n; i++) {
scanf("%d", &num);
p1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p1->next->val = num;
p1->next->next = NULL;
p1 = p1->next;
}
for (int i = 0; i < m; i++) {
scanf("%d", &num);
p2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p2->next->val = num;
p2->next->next = NULL;
p2 = p2->next;
}
struct ListNode* res = mergeList(l1->next, l2->next);
// 去除重复数
int hash[10001] = {0}; // 哈希表,假设节点值范围在 [0, 10000]
struct ListNode* cur = res;
struct ListNode* pre = NULL;
while (cur) {
if (hash[cur->val]) { // 如果当前节点值已经出现过,则删除当前节点
pre->next = cur->next;
free(cur);
cur = pre->next;
} else { // 如果当前节点值没有出现过,则将其加入哈希表,并继续遍历下一个节点
hash[cur->val] = 1;
pre = cur;
cur = cur->next;
}
}
// 输出结果
cur = res;
while (cur) {
printf("%d ", cur->val);
cur = cur->next;
}
// 释放内存
cur = res;
while (cur) {
struct ListNode* temp = cur;
cur = cur->next;
free(temp);
}
return 0;
}