-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Add Two Numbers.cpp
More file actions
33 lines (33 loc) · 951 Bytes
/
2. Add Two Numbers.cpp
File metadata and controls
33 lines (33 loc) · 951 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
// 17.03.24
// Runtime 11 ms Beats 96.26% of users with C++
// Memory 76.02 MB Beats 48.72% of users with C++
/**
* 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* addTwoNumbers(ListNode* it1, ListNode* it2) {
auto data = new ListNode();
auto prev = data;
auto quot = 0;
while (it1 || it2 || quot) {
auto sum = quot;
if (it1) sum += it1->val;
if (it2) sum += it2->val;
auto next = new ListNode(sum % 10);
quot = sum/10;
prev->next = next;
prev = prev->next;
if (it1) it1 = it1->next;
if (it2) it2 = it2->next;
}
return data->next;
}
};