-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLAdd2Numbers.cpp
More file actions
74 lines (67 loc) · 1.81 KB
/
LLAdd2Numbers.cpp
File metadata and controls
74 lines (67 loc) · 1.81 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Solution {
private:
Node* reverse(Node* head){
Node* curr = head;
Node* next = NULL;
Node* prev = NULL;
while(curr != NULL){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void insertAtTail(Node* &head, Node *&tail, int val){
Node* temp = new Node(val);
if(head == NULL){
head = temp;
tail = temp;
return;
}
else{
tail->next = temp;
tail = temp;
}
}
Node* add(Node* first, Node* second){
int carry = 0;
Node* addHead = NULL;
Node* addTail = NULL;
while(first != NULL || second != NULL || carry != 0){
int val1 = 0;
if(first != NULL){
val1 = first->data;
}
int val2 = 0;
if(second != NULL){
val2 = second->data;
}
int sum = carry + val1 + val2;
int digit = sum%10;
insertAtTail(addHead,addTail,digit);
carry = sum/10;
if(first != NULL){
first = first->next;
}
if(second != NULL){
second = second->next;
}
}
return addHead;
}
public:
// Function to add two numbers represented by linked list.
Node* addTwoLists(Node* num1, Node* num2) {
num1 = reverse(num1);
num2 = reverse(num2);
Node* ans = add(num1, num2);
ans = reverse(ans);
while(ans->data == 0 && ans->next != NULL){
Node* temp = ans;
delete temp;
ans = ans->next;
}
return ans;
}
};