-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_two_numbers_of_LL.cpp
49 lines (44 loc) · 1.33 KB
/
add_two_numbers_of_LL.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
// Given two Linked Lists with integers in reverse order, add them and return the sum as a linked list.
// Example - 543 + 657 = 1200
// list1 = 3 -> 4 -> 5
// list2 = 7 -> 5 -> 6
// ans = 0 -> 0 -> 2 -> 1
//Algorithm:
// 1. Use one pointer to update the sum. I used L1.
// 2. Insert new node if L1->next is null, until the sum is zero && L1->next is null && L2 is null.
// 3. Store the previous of current L1 before updating it to the next.
// 4. Delete the last node of L1 using prev.
//Code
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
};
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
int carry = 0;
ListNode* ans = l1;
ListNode* prev;
while(l1 || l2 )
{
int temp = 0;
if(l1)
temp += l1->val;
if(l2)
temp += l2->val;
temp += carry;
if(temp == 0 && !l1->next && !l2)
break;
l1->val = temp % 10;
carry = temp / 10;
prev = l1;
if(!l1->next)
l1->next = new ListNode();
l1 = l1->next;
if(l2)
l2 = l2->next;
}
prev->next = nullptr;
return ans;
}