-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0445 : Add Two Numbers II.cpp
62 lines (60 loc) · 1.32 KB
/
0445 : Add Two Numbers II.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
54
55
56
57
58
59
60
61
62
/*
Algorithm : Linked List Iteration
Language : C++
Time Complexity : O(n)
Space Complexity : O(n)
Date of Submission : 17/March/2023
*/
class Solution
{
private:
ListNode *reverse(ListNode* q)
{
ListNode *p = NULL;
ListNode *temp = q->next;
while(temp != NULL)
{
q->next = p;
p = q;
q = temp;
temp = q->next;
}
q->next = p;
return q;
}
ListNode *func(ListNode *l1, ListNode *l2)
{
ListNode *temp = NULL;
int carry = 0, sum = 0;
while(l1 || l2 || carry)
{
if(l1 != NULL)
{
sum += l1->val;
l1 = l1->next;
}
if(l2 != NULL)
{
sum += l2->val;
l2 = l2->next;
}
if(carry != 0)
{
sum += carry;
}
ListNode* List = new ListNode(sum%10);
List->next = temp;
temp = List;
carry = sum/10;
sum = 0;
}
return temp;
}
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode* List1 = reverse(l1);
ListNode* List2 = reverse(l2);
return func(List1, List2);
}
};