首页 > 解决方案 > LinkedList,向量构造函数的内存溢出?

问题描述

此代码使用linkedList 添加两个和整数。我在结构 ListNode中添加了一个新的构造函数,以便将两个向量 A 和 B 作为输入。

      ListNode(vector<int> array)
      {
        vector<int>::iterator itr = array.begin();
      ListNode *t = nullptr;
        for (; itr < array.end(); itr++) {
            t->val = *itr;
            t = t->next;
        }
      }

这是一个奇怪的溢出??

#include <iostream>
#include <vector>
using namespace std;
/*
  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) {}
      
      ListNode(vector<int> array)
      {
        vector<int>::iterator itr = array.begin();
      ListNode *t = nullptr;
        for (; itr < array.end(); itr++) {
            t->val = *itr;
            t = t->next;
        }
      }
      
  };
 
class Solution {
public:
    Solution(){};
    
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *p = l1;
        ListNode *q = l2;
        ListNode *dummyHead = new ListNode(0);
        ListNode *current = dummyHead;
        int carry = 0;
        
        while (p != NULL || q != NULL) {
            int x = (p != NULL) ? p->val : 0;
            int y = (q != NULL) ? q->val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            
            current->next = new ListNode(sum % 10);
            current = current -> next;
            
            if (p != NULL) {
                p = p -> next;
            }
            
            if (q != NULL) {
                q = q -> next;
            }
        }
        
        if (carry > 0) {
            current -> next = new ListNode(carry);
        }
        return dummyHead->next;
        
    }
};



int main(int argc, const char * argv[]) {
    Solution *sol = nullptr;
    vector<int> A = {2,4,3};
    vector<int> B = {5,6,4};
    ListNode *list1= new ListNode(A);
    ListNode *list2= new ListNode(B);

    sol->addTwoNumbers(list1,list2);
    
    
    return 0;
}

测试用例:

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

标签: c++

解决方案


在您访问之前,您的ListNode构造函数不会创建新ListNode的 fort指向t->val。它甚至也没有尝试初始化this.

尝试更多类似的东西:

struct ListNode {
    int val = 0;
    ListNode *next = nullptr;

    ListNode(int x = 0, ListNode *next = nullptr) : val(x), next(next) {}
      
    ListNode(const vector<int> &array)
    {
        if (!array.empty()) {
            auto itr = array.begin();
            val = *itr++;
            ListNode **t = &next;
            while (itr != array.end()) {
                *t = new ListNode(*itr++);
                t = &((*t)->next);
            }
        }
    }
};

话虽如此,拥有这样一个构造函数ListNode真的没有意义。一个列表节点应该只关心它自己的数据,而不关心它周围节点的数据。这种列表构造确实属于一个单独的List类,该类包装了一个 s 链ListNode,例如:

#include <iostream>
#include <vector>
#include <utility>

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

  public:
      List() = default;

      List(const vector<int> &vec)
      {
          ListNode **t = &head;
          for (int val : vec) {
            *t = new ListNode(val);
            t = &((*t)->next);
        }
      }

      List addNumber(const List &l) const {
          ListNode *p = head;
          ListNode *q = l.head;
          List dummyList;
          ListNode** current = &(dummyList.head);
          int carry = 0;
        
          while (p || q) {
              int x = (p) ? p->val : 0;
              int y = (q) ? q->val : 0;
              int sum = carry + x + y;
              carry = sum / 10;
            
              *current = new ListNode(sum % 10);
              current = &((*current)->next);
            
              if (p) {
                  p = p->next;
              }
            
              if (q) {
                  q = q->next;
              }
          }
        
          if (carry > 0) {
              *current = new ListNode(carry);
          }

          return dummyList;
      }

      // what follows is stuff needed for compliance with the "Rule of 3/5/0":
      // https://en.cppreference.com/w/cpp/language/rule_of_three

      List(const List &list)
      {
          ListNode **t = &head;
          for(ListNode *n = list.head; n; n = n->next) {
            *t = new ListNode(n->val);
            t = &((*t)->next);
        }
      }

      List(List &&list)
      {
          std::swap(head, list.head);
      }

      ~List()
      {
          ListNode *n;
          for (ListNode *t = head; t; t = n) {
              n = t->next;
              delete t;
          }
      }

      List& operator=(List rhs) {
          List temp(std::move(rhs));
          std::swap(head, temp.head);
          return *this;
      }
  };

class Solution {
public:
    List addTwoNumbers(const List &l1, const List &l2) {
        return l1.addNumber(l2);
    }
};

int main(int argc, const char * argv[]) {
    Solution sol;
    vector<int> A = {2,4,3};
    vector<int> B = {5,6,4};
    List list1(A);
    List list2(B);

    List result = sol.addTwoNumbers(list1, list2);
    
    return 0;
}

推荐阅读