```
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode *svalue = new ListNode(0);
ListNode *sum;
sum = svalue;
if (l1 != NULL || l2 != NULL) {
while (l1->next != NULL || l2->next != NULL) {
if (l1->next != NULL && l2->next != NULL) {
sum->next = new ListNode(0);
sum->val = (l1->val + l2->val + carry) % 10;
carry = (l1->val + l2->val + carry) / 10;
sum = sum->next;
sum->next = NULL;
l1 = l1->next;
l2 = l2->next;
}
else if (l1->next == NULL) {
sum->next = new ListNode(0);
sum->val = (l2->val + carry) % 10;
carry = (l2->val + carry) / 10;
sum = sum->next;
sum->next = NULL;
l2 = l2->next;
}
else {
sum->next = new ListNode(0);
sum->val = (l1->val + carry) % 10;
carry = (l1->val + carry) / 10;
sum = sum->next;
sum->next = NULL;
l1 = l1->next;
}
}
}
if (carry > 0) {
sum->next = new ListNode(0);
sum->val = carry;
sum = sum->next;
sum->next = NULL;
}
return svalue;
```

}