/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
ListNode* addInList(ListNode* head1, ListNode* head2) {
vector<int> v1;
vector<int> v2;
for (ListNode* cur = head1; cur; cur = cur->next) {
v1.push_back(cur->val);
}
for (ListNode* cur = head2; cur; cur = cur->next) {
v2.push_back(cur->val);
}
int i = v1.size() - 1;
int j = v2.size() - 1;
int jin = 0;
ListNode* rethead = new ListNode(0);
while (i >= 0 && j >= 0) {
int temp = v1[i--] + v2[j--] + jin;
jin = temp > 9 ? 1 : 0;
temp %= 10;
ListNode* cur = new ListNode(temp);
cur->next = rethead->next;
rethead->next = cur;
}
while (i >= 0) {
int temp = v1[i--] + jin;
jin = temp > 9 ? 1 : 0;
temp %= 10;
ListNode* cur = new ListNode(temp);
cur->next = rethead->next;
rethead->next = cur;
}
while (j >= 0) {
int temp = v2[j--] + jin;
jin = temp > 9 ? 1 : 0;
temp %= 10;
ListNode* cur = new ListNode(temp);
cur->next = rethead->next;
rethead->next = cur;
}
if(jin==1){
rethead->val = 1;
return rethead;
}else {
return rethead->next;
}
}
};