c++ - 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.
解决方案
在您访问之前,您的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;
}
推荐阅读
- c++ - 如何使用 C++ 在 Windows 中检测 RDP 会话显示模式和使用的协议(TCP/UDP)
- javascript - javascript如何跳过为方法传递参数?
- html - 多个手风琴(首先默认打开+打开另一个手风琴时自动关闭)
- javascript - 有什么办法破解 react-router-Dom 私有路由?
- python - PYTHON 为什么我的 python IDLE 不能将 numpy 识别为模块,尽管在 cmd 中运行 pythob 时可以正常使用 numpy?
- javascript - 如何正确将 scss 文件导入 VueJS 项目
- ruby-on-rails - Sidekiq 6 仅在我进入 Web 控制台时进行轮询
- r - 如何重新排序 heatmap.2 中的列
- django - 在 Django 中发出 ajax 发布请求时未调用服务器
- javascript - 第二个ajax请求不应该等到第一个完成它的执行