/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
//链表操作
ListNode *newhead = new ListNode(-1);
ListNode *newl = newhead;
// newhead = newl;
int jinwei = 0;
while (l1 != NULL&&l2 != NULL)
{
newl->next = new ListNode((l1->val+l2->val + jinwei) % 10);
jinwei = (l1->val + l2->val + jinwei) / 10;
l1 = l1->next;
l2 = l2->next;
newl = newl->next;
}
while (l1 != NULL)
{
//ListNode *c = new ListNode(0);
newl->next = new ListNode((l1->val + jinwei) % 10);
jinwei = (l1->val+jinwei) / 10;
l1 = l1->next;
//l2 = l2->next;
newl = newl->next;
}
while (l2 != NULL)
{
newl->next = new ListNode((l2->val + jinwei) % 10);
jinwei = (l2->val + jinwei) / 10;
l2 = l2->next;
newl = newl->next;
}
if (jinwei != 0)
{
//jinwei = (l2->val + jinwei) / 10;
newl->next = new ListNode(jinwei);
}
return newhead->next;
}
};