# Does this question require inplace or allocate extra nodes as few as possible?

• Does this question require inplace or allocate extra nodes as few as possible?

Below is my code.

/**

• struct ListNode {

• ``````int val;
``````
• ``````ListNode *next;
``````
• ``````ListNode(int x) : val(x), next(NULL) {}
``````
• };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
if (l1==NULL)
return l2;
if (l2==NULL)
return l1;

`````` int c=0;
int s;

ListNode *p1 = l1;
ListNode *p2 = l2;
ListNode *preNode;
while (p1!=NULL && p2!=NULL)
{
s=p1->val+p2->val+c;
c=s/10;
s=s%10;
p1->val=s;
p2->val=s;
preNode = p1;
p1=p1->next;
p2=p2->next;

}

ListNode *t,*r;
if(p1!=NULL)
{
t=p1;
r=l1;
}
else if (p2!=NULL)
{
t=p2;
r=l2;
}
else if (p1==NULL && p2==NULL)
{
if (c)
{
preNode->next = new ListNode(1);
}
return l1;
}
``````
``````        while(t!=NULL)
{
s=t->val+c;
c=s/10;
s=s%10;
t->val=s;
preNode = t;
t = t->next;
}

if (c)
{
preNode->next = new ListNode(1);
}

return r;

}
``````

};

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.