https://leetcode-cn.com/problems/add-two-numbers/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == nullptr && l2) {
return l2;
}
if (l2 == nullptr && l1) {
return l1;
}
ListNode* resHead;
ListNode* res = new ListNode();
resHead = res;
int temp_value = 0;
while (l1 || l2) {
int l1_value = INT_MAX;
int l2_value = INT_MAX;
if (l1) {
l1_value = l1->val;
temp_value += l1_value;
l1 = l1->next;
}
if (l2) {
l2_value = l2->val;
temp_value += l2_value;
l2 = l2->next;
}
ListNode* node = new ListNode(temp_value % 10);
temp_value = temp_value / 10;
res->next = node;
res = res->next;
}
if (temp_value) {
ListNode* node = new ListNode(temp_value);
res->next = node;
res = res->next;
}
return resHead->next;
}