Skip to content

Commit 316ab91

Browse files
committedOct 21, 2018
Add description to question2
1 parent 89c5a9b commit 316ab91

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed
 

‎solution/002.Add Two Numbers/README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,45 @@ class Solution {
9898
return res.next;
9999
}
100100
}
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+
```

0 commit comments

Comments
 (0)
Please sign in to comment.