-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_002_Add_Two_Numbers.cpp
More file actions
97 lines (94 loc) · 2.16 KB
/
_002_Add_Two_Numbers.cpp
File metadata and controls
97 lines (94 loc) · 2.16 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <string>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#define ALL(x) ((x).begin()),((x).end())
#define RESET(a,b) memset(a,b, sizeof a)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define LEN(X) strlen(X)
#define FI(a,b,c) for(int a=b; a<c; a++)
#define FD(a,b,c) for(int a=b; a>c; a--)
using namespace std;
typedef long long ll;
typedef long long LL;
typedef double db;
typedef double D;
typedef long double ld;
typedef long double LD;
/*********default*********/
static auto x = [](){
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(NULL);
return 0;
}();
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {
}
};
class InitList {
public:
ListNode* createList(vector<int> nums) {
if(nums.size() == 0) {
return NULL;
}
ListNode* head = new ListNode(nums[nums.size() - 1]);
ListNode* currentNode = head;
for(int i = nums.size() - 2; i >= 0; i--) {
currentNode->next = new ListNode(nums[i]);
currentNode = currentNode->next;
}
return head;
}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(0);
ListNode* currentNode = head;
int carry = 0;
while(l1 != NULL || l2 != NULL || carry != 0) {
int x = (l1 != NULL) ? l1->val : 0;
int y = (l2 != NULL) ? l2->val : 0;
int sum = carry + x + y;
carry = sum / 10;
currentNode->next = new ListNode(sum % 10);
currentNode = currentNode->next;
if(l1 != NULL) {
l1 = l1->next;
}
if(l2 != NULL) {
l2 = l2->next;
}
}
return head->next;
}
};
int main() {
vector<int> nums1 = {5};
vector<int> nums2 = {5};
InitList I;
ListNode* l1 = I.createList(nums1);
ListNode* l2 = I.createList(nums2);
Solution s;
ListNode* head = s.addTwoNumbers(l1, l2);
while(head) {
cout << head->val << " ";
head = head->next;
}
return 0;
}