``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode *res = l1,*pre = l1;
while(l1&&l2)
{
l1->val += l2->val + carry;
carry = 0;
if(l1->val >= 10)
{
l1->val %= 10;
carry = 1;
}
pre = l1;
l1 = l1->next;
l2 = l2->next;
}
if(carry)
{
ListNode *last = new ListNode(1);
last->next = NULL;
pre->next = last;
}
return res;
}
};``````

``````/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = new ListNode(-1), *p1 = l1, *p2 = l2, *p = head;
int carry = 0;
while(p1 || p2 || carry)
{
int sum = 0;
if(p1)
{
sum += p1->val;
p1 = p1->next;
}

if(p2)
{
sum += p2->val;
p2 = p2->next;
}

sum += carry;

ListNode *node = new ListNode(sum %10);
(sum >= 10)? carry = 1:carry = 0;
p->next = node;
p = p->next;
}