leetcode中的c ++内存泄漏添加两个数字

c++ memory leak in leetcode add two numbers

尝试使用 C++ 完成 leetcode 挑战,这里是新手。

下面是我的代码

/** Problem 2, Add two numbers, done 04/07/2021


You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:

Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:

Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:

    The number of nodes in each linked list is in the range [1, 100].
    0 <= Node.val <= 9
    It is guaranteed that the list represents a number that does not have leading zeros.
*/

#include "../common_headers.hpp"

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *nxt) : val(x), next(nxt){}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2){
    ListNode* head;
    ListNode* node = new ListNode();
    head = node;
    int carry = 0;
    int v1 = 0, v2 = 0;
    while (l1 != nullptr || l2 != nullptr) {
        if(l1 != nullptr){
            v1 = l1->val;
            l1 = l1->next;
        } else v1 = 0;
        if(l2 != nullptr) {
            v2 = l2->val;
            l2 = l2->next;
        } else v2 = 0;

        int sum = v1 + v2 + carry;
        if(sum > 9){
            sum = sum % 10;
            carry = 1;
        } else {
            carry = 0;
        }
        ListNode* newNode = new ListNode(sum);
        node->next = newNode;
        node = node->next;
    }
    if(carry == 1){
        ListNode* newNode = new ListNode(1);
        node->next = newNode;
    }
    return head->next;
}

int main(){
    ListNode* n3 = new ListNode(3);
    ListNode* n2 = new ListNode(4, n3);
    ListNode* n1 = new ListNode(2, n2);

    ListNode* m3 = new ListNode(4);
    ListNode* m2 = new ListNode(6, m3);
    ListNode* m1 = new ListNode(5, m2);

    ListNode* ln = addTwoNumbers(n1, m1);
    
    while (ln != nullptr){
        std::cout<<ln->val<<" ";
        ln = ln->next;
    }
    std::cout<<std::endl;
    return 0;
}

已编译并给出正确答案,但是,g++ 告诉我存在内存泄漏。我知道我在这里使用了 new 运算符,但是在得到返回的答案之前我无法删除它们。

请问我该如何解决这个问题?下面是错误信息

==3256==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x401295 in addTwoNumbers(ListNode*, ListNode*) /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:43
    #2 0x4016d8 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:84
    #3 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x4015c0 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:78
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x40169c in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:82
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 48 byte(s) in 3 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x401323 in addTwoNumbers(ListNode*, ListNode*) /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:64
    #2 0x4016d8 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:84
    #3 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x401652 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:81
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x401606 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:80
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x401576 in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:77
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff9d68f40a7 in operator new(unsigned long) (/lib64/libasan.so.6+0xb00a7)
    #1 0x40152c in main /home/xxx/Documents/Projects/leetcode/0001-0100/0002_Add_Two_Numbers.cpp:76
    #2 0x7ff9d62f0b74  (/lib64/libc.so.6+0x27b74)

在main的末尾删除它。在 for 循环之后但在“return 0;”之前。 主要内容应如下所示:

int main(){
    ...
    std::cout<<std::endl;

    delete n3;
    delete n2;
    delete n1;

    delete m3;
    delete m2;
    delete m1;

    ListNode* tmp;
    while (lnHead != nullptr) {
        tmp = lnHead;
        lnHead = lnHead->next;
        delete tmp;
    }

    return 0;
}

像这样的 addTwoNumbers:

...
ListNode* tmp = head->next;
delete head;
return tmp;