forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path445.cpp
More file actions
46 lines (46 loc) · 1.16 KB
/
445.cpp
File metadata and controls
46 lines (46 loc) · 1.16 KB
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
37
38
39
40
41
42
43
44
45
46
/**
* 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) {
stack<int> l1Stack,l2Stack;
ListNode *list=NULL;
int x,y,carry=0;
while(l1 || l2){
if(l1){
l1Stack.push(l1->val);
l1=l1->next;
}
if(l2){
l2Stack.push(l2->val);
l2=l2->next;
}
}
while(!l1Stack.empty() || !l2Stack.empty() || carry){
x=0;
y=0;
if(!l1Stack.empty()){
x=l1Stack.top();
l1Stack.pop();
}
if(!l2Stack.empty()){
y=l2Stack.top();
l2Stack.pop();
}
x=x+y+carry;
carry=(int)x/10;
auto *temp=new ListNode(x%10);
temp->next=list;
list=temp;
}
return list;
}
};