-
Notifications
You must be signed in to change notification settings - Fork 39
/
BestSolution.cpp
52 lines (50 loc) · 1.53 KB
/
BestSolution.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//Given by leetcode
class Solution {
public:
//defining a prev pointer to store the address of previous node to insert at end
ListNode *prev;
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
//declaring a resultant linked list l3 as NULL
ListNode *l3 = NULL;
// initializing and declaring 2 variable sum and carry
int sum=0, carry=0;
//loop for extracing and storing of element
while(l1 != NULL || l2 != NULL){
//takes l1/l2->val else 0
int x = ((l1 != NULL) ? l1->val : 0);
int y = ((l2 != NULL) ? l2->val : 0);
sum = x + y + carry;
//if sum>10 then carry is 1
carry = sum / 10;
ListNode *newNode = new ListNode(sum % 10);
/* inserting at the end as seen if condition runs only once
then else to store at end*/
if(l3 == NULL){
l3 = newNode;
}
else
prev->next = newNode;
prev = newNode;
if(l1 != NULL){
l1 = l1->next;
}
if(l2 != NULL){
l2 = l2->next;
}
}
if(carry > 0){
// if carry if left at the end then to store it at end
ListNode *newNode = new ListNode(carry);
prev->next = newNode;
}
return l3;
}
};