1561/1563 test cases passed
problem discription:
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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. link to problem: https://leetcode.com/problems/add-two-numbers-ii/
you dont need to provide me with code(would be helpful), just the answer why do I get wrong answer for next questions.
void insert_begging(struct ListNode **root,unsigned __int128 val){
struct ListNode *new_node = malloc(sizeof(struct ListNode));
new_node->val = val;
if(*root == NULL){
new_node->next = NULL;
*root = new_node;
return;
}
new_node->next = *root;
*root = new_node;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode* result = NULL;
struct ListNode* curr = l1;
unsigned __int128 sum = 0;
unsigned __int128 sum2 = 0;
while(curr!=NULL){
sum=sum*10+curr->val;
curr=curr->next;
}
struct ListNode* curr2 = l2;
while(curr2!= NULL){
sum2=sum2*10+curr2->val;
curr2=curr2->next;
}
unsigned __int128 rsum = sum+sum2;
unsigned __int128 digit = 0;
if(rsum == 0){
struct ListNode* result = malloc(sizeof(struct ListNode));
result->val = rsum;
result->next = NULL;
return result;
}
while (rsum > 0) {
int digit = rsum % 10;
insert_begging(&result, digit);
rsum /= 10;
}
return result;
}
so I spent 1 hour trying to solve this problem, with no luck
this is the test i failed:
l1 = [2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9]
l2 = [5,6,4,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,2,4,3,9,9,9,9]
result is supposed to be:
[8,0,7,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,6,4,8,7,2,4,3,8]
firstly I got int overflows even with unsigned long long type so I changed it to unsigned __int128 but i got this:
[1,7,0,4,9,1,2,5,7,9,2,0,1,8,3,3,6,8,2,6,4,9,7,6,4,3,9,5,8,5,1,6,4,5,3,5,7,9,8]