JerryChengCSU 2022-04-13 00:13
浏览 90
已结题

力扣92.反转链表 Ⅱ 代码写好后无法提交,但是在VS和力扣的playground都能运行

问题遇到的现象和发生背景

能在VS和力扣的playground运行,但是提交代码的时候提示

==42==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x602000000170
#3 0x7fae665680b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x602000000170 is located 0 bytes inside of 16-byte region [0x602000000170,0x602000000180)
allocated by thread T0 here:
#3 0x7fae665680b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
==42==HINT: if you don't care about these errors you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0
==42==ABORTING

问题相关代码,请勿粘贴截图

/**

  • Definition for singly-linked list.
  • struct ListNode {
  • int val;
    
  • ListNode *next;
    
  • ListNode() : val(0), next(nullptr) {}
    
  • ListNode(int x) : val(x), next(nullptr) {}
    
  • ListNode(int x, ListNode *next) : val(x), next(next) {}
    
  • };
  • /

class Solution
{

public:
ListNode* reverseBetween(ListNode* head, int left, int right)
{

ListNode* p;//用于探测链表长度;
ListNode* q;//用于将链表值赋给数组;
ListNode* r;//用于指示原链表片段右端;
ListNode* l;//用于指示原链表片段左端
ListNode* l1;//用于指示新链表片段左端;
ListNode* r1;//用于扩建新链表,用于指示新链表片段右端;
p= head;
q = head;
l = head;
r = head;
int n = 1;
while(p->next != nullptr)
{
    n++;
    p = p->next;
}
int* array;
array = (int*)malloc(n*sizeof(int));
for (int i = 0; i<=n; i++)
{
    if(q->next != nullptr) {
        *(array+i) = q->val;
        q = q->next;
    }
    else { * (array + i) = q->val; break; }
}
r1 = (ListNode*)malloc(sizeof(ListNode));
r1->val = *(array+n-1);
r1->next = NULL;
for (int i = n; i > 1; i--)
{
    ListNode* q;
    q = (ListNode*)malloc(sizeof(ListNode));
    q->val =*(array+n-i);
    q->next = r1->next;
    r1->next = q;
}
l1 = r1;
int right1 = n-left+1;
int left1 = n-right+1;
for (left; left > 2; left--)//原
{
    l = l->next;
}
for (right; right > 0; right--)//原
{
    r = r->next;
}
for(left1; left1 >1; left1--)//新
{
    l1 = l1->next;
}
for (right1; right1 > 1; right1--)//新
{
    r1 = r1->next;
}
l->next = l1;
r1->next = r;

return head;

}
};

有没有人能把那个错误忽略了或者进一步改进

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月21日
    • 创建了问题 4月13日

    悬赏问题

    • ¥15 券商软件上市公司信息获取问题
    • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
    • ¥15 Android studio AVD启动不了
    • ¥15 陆空双模式无人机怎么做
    • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
    • ¥15 C#中的编译平台的区别影响
    • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
    • ¥15 电脑蓝屏logfilessrtsrttrail问题
    • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
    • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?