Skip to content

Commit 89c5a9b

Browse files
committed
Add CPP solution to the question2 and create new dict for the question33
1 parent 1c26430 commit 89c5a9b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public:
3+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
4+
5+
ListNode *ans_l = new ListNode(0);
6+
ListNode *head = ans_l;
7+
int tmp = 0;
8+
while(l1 != NULL && l2 != NULL){
9+
tmp += l1->val + l2->val;
10+
ans_l->next = new ListNode(tmp % 10);
11+
tmp = tmp / 10;
12+
ans_l = ans_l->next;
13+
l1 = l1->next;
14+
l2 = l2->next;
15+
}
16+
17+
while(l1 != NULL){
18+
tmp += l1->val;
19+
ans_l->next = new ListNode(tmp % 10);
20+
tmp = tmp / 10;
21+
ans_l = ans_l->next;
22+
l1 = l1->next;
23+
}
24+
25+
while(l2 != NULL){
26+
tmp += l2->val;
27+
ans_l->next = new ListNode(tmp % 10);
28+
tmp = tmp / 10;
29+
ans_l = ans_l->next;
30+
l2 = l2->next;
31+
}
32+
33+
if(tmp)ans_l->next = new ListNode(tmp);
34+
35+
return head->next;
36+
}
37+
};

0 commit comments

Comments
 (0)