File tree 1 file changed +37
-0
lines changed
solution/002.Add Two Numbers
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments