-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdd_Two_Numbers.cpp
More file actions
31 lines (30 loc) · 819 Bytes
/
Add_Two_Numbers.cpp
File metadata and controls
31 lines (30 loc) · 819 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
/**
* 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) {
return addNodes(l1,l2,0);
}
ListNode* addNodes(ListNode* l1, ListNode* l2, int eb) {
if(!l1 && !l2 && eb == 0) return NULL;
int x=0, y=0;
if(l1){
x=l1->val;
}
if(l2){
y=l2->val;
}
int v = x+y+eb;
ListNode *h = new ListNode(v%10);
h->next = addNodes(l1? l1->next: NULL , l2? l2->next: NULL, v/10);
return h;
}
};