模拟
/**
* 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) {
int jin = 0;
ListNode* t1 = l1;
ListNode* t2 = l2;
ListNode* head = new ListNode();
ListNode* trans = head;
while(t1 != nullptr && t2 != nullptr){
int sum = jin + t1 ->val + t2 ->val;
t1 -> val = sum % 10;
jin = sum / 10;
trans ->next = t1;
trans = t1;
t1 = t1 ->next;
t2 = t2 ->next;
}
while(t1 != nullptr){
int sum = jin + t1 ->val;
t1 -> val = sum % 10;
jin = sum / 10;
trans ->next = t1;
trans = t1;
t1 = t1 ->next;
}
while(t2 != nullptr){
int sum = jin + t2 ->val;
t2 -> val = sum % 10;
jin = sum / 10;
trans ->next = t2;
trans = t2;
t2 = t2 ->next;
}
if(jin == 1) trans ->next = new ListNode(1);
return head ->next;
}
};