-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd two number2.cpp
53 lines (52 loc) · 1.86 KB
/
Add two number2.cpp
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
47
48
49
50
51
52
53
/**
* 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) {}
* };
*/
//addTwoNumbers
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *ptr1 = l1, *ptr2 = l2;
while(ptr1 != NULL || ptr2 != NULL){
if(ptr1 == NULL){
ListNode *newNode = new ListNode(0);//Adding 0's to end of the first list if its size is less than second list
newNode->next = l1;
l1 = newNode;
ptr2 = ptr2->next;
}
else if(ptr2 == NULL){
ListNode *newNode = new ListNode(0); //Adding 0's to end of the second list if its size is less than first list
newNode->next = l2;
l2 = newNode;
ptr1 = ptr1->next;
}
else{
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
}
int carry = 0;
ListNode *dummy = new ListNode(-1);
dummy->next = addTwoDigit(l1, l2, carry);
if(carry != 0){
ListNode *newNode = new ListNode(carry);
newNode->next = dummy->next;
dummy->next = newNode;
}
return dummy->next;
}
ListNode* addTwoDigit(ListNode* l1, ListNode* l2, int &carry){ //recursion is used to perform addition
if(l1 == NULL && l2 == NULL) return NULL;
ListNode *newNode = new ListNode(-1);
newNode->next = addTwoDigit(l1->next, l2->next, carry);
newNode->val = (l1->val + l2->val + carry) % 10;
carry = (l1->val + l2->val + carry) / 10;
return newNode;
}
};