-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_2_add_two_numbers.cpp
More file actions
44 lines (39 loc) · 1.06 KB
/
problem_2_add_two_numbers.cpp
File metadata and controls
44 lines (39 loc) · 1.06 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 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* root = new ListNode(0);
ListNode* curr = root;
ListNode* p_l1 = l1;
ListNode* p_l2 = l2;
int carry = 0;
while (p_l1 || p_l2) {
int x = (p_l1 != NULL) ? p_l1->val : 0;
int y = (p_l2 != NULL) ? p_l2->val : 0;
int sum = x + y + carry;
carry = sum/10;
curr->val = sum%10;
if (p_l1 != NULL) {
p_l1 = p_l1->next;
}
if (p_l2 != NULL) {
p_l2 = p_l2->next;
}
if (p_l1 || p_l2) {
curr->next = new ListNode(0);
curr = curr->next;
}
}
if (carry == 1) {
curr->next = new ListNode(1);
}
return root;
}
};