-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathadd-two-numbers.cpp
More file actions
34 lines (33 loc) · 930 Bytes
/
add-two-numbers.cpp
File metadata and controls
34 lines (33 loc) · 930 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
28
29
30
31
32
33
34
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~
--||author : codechaser||--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode sum{0};
auto curr = ∑
int carry = 0;
while (l1 || l2 || carry)
{
int a = l1 ? l1->val : 0;
int b = l2 ? l2->val : 0;
curr->next = new ListNode((carry + a + b) % 10);
carry = (carry + a + b) / 10;
l1 = l1 ? l1->next : nullptr;
l2 = l2 ? l2->next : nullptr;
curr = curr->next;
}
return sum.next;
}
};
/*
|---------------------------------------------------|
||| https://codeforces.com/profile/codechaser |||
||| https://www.codechef.com/users/codechaser |||
||| https://github.com/code-chaser |||
|---------------------------------------------------|
*/