https://leetcode.com/problems/add-two-numbers/submissions/1259689057
failed test case
//my code in c.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int64_t sum(struct ListNode *root)
{
if(root==NULL)
{
return 0;
}
return (sum(root->next)*10) + root->val;
}
struct ListNode* generateNode(int num)
{
struct ListNode *p;
p=(struct ListNode*)malloc(sizeof(struct ListNode));
if(num<=0)
{
return NULL;
}
p->val=num%10;
p->next=generateNode(num/10);
return p;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
int64_t a1,a2;
a1=sum(l1);
a2=sum(l2);
//printf("%d",a1+a2);
struct ListNode *p;
p=(struct ListNode*)malloc(sizeof(struct ListNode));
p->val=0;
p->next=NULL;
if(a1+a2!=0){
p=generateNode(a1+a2);
}
return p;
}
I the above is a code I wrote.
I think the error is due lack of storage space.
I tried my best if you know the answer please share me.
New contributor
Ponaalagar OK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.