-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-5-add-two-numbers.cpp
More file actions
27 lines (27 loc) · 909 Bytes
/
Copy pathDay-5-add-two-numbers.cpp
File metadata and controls
27 lines (27 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
class Solution {
public:
ListNode* solve(ListNode* num1, ListNode* num2, int carry){
if(num1==NULL && num2==NULL && carry == 0) return NULL;
int first = (num1 != NULL) ? num1->val : 0;
int second = (num2 != NULL) ? num2->val : 0;
int sum = first + second + carry;
carry = sum / 10;
sum = sum % 10;
ListNode* new_node = new ListNode(sum);
new_node->next = solve((num1 != NULL) ? num1->next : NULL, (num2 != NULL) ? num2->next : NULL, carry);
return new_node;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
return solve(l1, l2, 0);
}
};