-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.cc
More file actions
40 lines (33 loc) · 1.42 KB
/
AddTwoNumbers.cc
File metadata and controls
40 lines (33 loc) · 1.42 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
/**
* 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 * dummy= new ListNode();
ListNode * res= dummy;
int carry=0;
while(l1 || l2){
int sum1=(l1 ? l1->val: 0); // if l1 != NULL then assign l1->val to sum1 else assign 0
int sum2=(l2 ? l2->val: 0); // same if else statement
int sum= sum1+sum2+carry; // carry obtained after addition operations
int mod= sum%10; // if sum>10 then we cant assign anything >10 so we get the modulus of the sum by ten
res-> next= new ListNode(mod); // res->next ------ using constructor, res -> val = mod , res->next NULL
carry= sum/10; // carry can be only 1 or 0 here we assign carry 0
res= res->next; // going to next pointer
l1= (l1 ? l1->next: l1); //check if is null or not
l2= (l2 ? l2->next: l2); // same as above
}
if(carry==1){
res->next= new ListNode(1);
}
return dummy->next;
}
};