File tree 1 file changed +42
-1
lines changed
solution/002.Add Two Numbers
1 file changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -98,4 +98,45 @@ class Solution {
98
98
return res. next;
99
99
}
100
100
}
101
- ```
101
+ ```
102
+ #### CPP
103
+ ``` CPP
104
+ class Solution {
105
+ public:
106
+ ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
107
+
108
+ ListNode *ans_l = new ListNode(0);
109
+ ListNode *head = ans_l;
110
+ int tmp = 0;
111
+ while(l1 != NULL && l2 != NULL){
112
+ tmp += l1->val + l2->val;
113
+ ans_l->next = new ListNode(tmp % 10);
114
+ tmp = tmp / 10;
115
+ ans_l = ans_l->next;
116
+ l1 = l1->next;
117
+ l2 = l2->next;
118
+ }
119
+
120
+ while (l1 != NULL ){
121
+ tmp += l1->val;
122
+ ans_l->next = new ListNode(tmp % 10);
123
+ tmp = tmp / 10;
124
+ ans_l = ans_l->next;
125
+ l1 = l1->next;
126
+ }
127
+
128
+ while(l2 != NULL){
129
+ tmp += l2->val;
130
+ ans_l->next = new ListNode(tmp % 10);
131
+ tmp = tmp / 10;
132
+ ans_l = ans_l->next;
133
+ l2 = l2->next;
134
+ }
135
+
136
+ if(tmp)ans_l->next = new ListNode(tmp);
137
+
138
+ return head->next;
139
+ }
140
+ };
141
+
142
+ ```
You can’t perform that action at this time.
0 commit comments