-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#0002.add-two-numbers.cpp
More file actions
36 lines (34 loc) · 950 Bytes
/
#0002.add-two-numbers.cpp
File metadata and controls
36 lines (34 loc) · 950 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *prehead = new ListNode();
ListNode *pre = prehead;
ListNode *cur1 = l1, *cur2 = l2;
int digit = 0;
while(cur1 || cur2 || digit > 0){
if(cur1){
digit += cur1->val;
cur1 = cur1->next;
}
if(cur2){
digit += cur2->val;
cur2 = cur2->next;
}
ListNode *tmp = new ListNode(digit%10);
pre->next = tmp;
pre = pre->next;
digit/=10;
}
return prehead->next;
}
};