Witam ,mam takie kod jak poniżej do zadania "Add Two Numbers" ze strony LeetCode i moje rozwiązanie nie jest zakceptowane. Dostaję taki komunikat jak poniżej. O co z tym chodzi i jak się pozbyć tego błędu?
Line 1061: Char 9: runtime error: addition of unsigned offset to 0x7fffbd5c55f0 overflowed to 0x7fffbd5c55ef (basic_string.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:1070:9
Kod:
/**
* 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) {}
* };
*/
#include <string>
#include <sstream>
class Solution {
public:
void push(ListNode** head_ref, signed long new_data)
{
ListNode* new_node = new ListNode();
new_node->next = NULL;
new_node->val = new_data;
if(*head_ref == NULL)
{
*head_ref = new_node;
return;
}
ListNode* curr = *head_ref;
while(curr->next != NULL){
curr = curr->next;
}
curr->next = new_node;
}
void reverseStr(string& str)
{
int n = str.length();
for (int i = 0; i < n / 2; i++)
swap(str[i], str[n - i - 1]);
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
std::string n1, n2;
signed long a = 0, b = 0;
while (l1 != nullptr)
{
n1 += std::to_string(l1->val);
l1 = l1->next;
}
while (l2 != nullptr)
{
n2 += std::to_string(l2->val);
l2 = l2->next;
}
reverseStr(n1);
reverseStr(n2);
std::stringstream ss1;
ss1 << n1;
ss1>> a;
std::stringstream ss2;
ss2 << n2;
ss2 >> b;
auto z = a + b;
std::string answer = std::to_string(z);
ListNode* root = NULL;
for(auto i = answer.length()-1; i >= 0; i--)
{
char t = answer[i];
signed long temp = t - '0';
push(&root, temp);
}
return root;
}
};