-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers2.cpp
122 lines (120 loc) · 3.2 KB
/
AddTwoNumbers2.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode * reverse(ListNode * head){
if(head == NULL || head->next == NULL){
return head;
}
ListNode * rest = reverse(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
l1 = reverse(l1);
l2 = reverse(l2);
ListNode * curr1 = l1;
ListNode * curr2 = l2;
ListNode * dummy = new ListNode(-1);
ListNode * curr = dummy;
int carry = 0;
while(curr1!=NULL && curr2!=NULL){
int x = carry + curr1->val + curr2->val;
curr->next = new ListNode(x%10);
carry = x/10;
curr = curr->next;
curr1=curr1->next;
curr2=curr2->next;
}
while(curr1){
int x = carry + curr1->val ;
curr->next = new ListNode(x%10);
carry = x/10;
curr = curr->next;
curr1 = curr1->next;
}
while(curr2){
int x = carry + curr2->val;
curr->next = new ListNode(x%10);
carry = x/10;
curr = curr->next;
curr2 = curr2->next;
}
if(carry){
curr->next= new ListNode(carry);
}
curr = dummy;
dummy = dummy->next;
return reverse(dummy);
}
};
//Iterative approach without modifying the original linked list
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode * reverseList(ListNode * head){
if(head== NULL || head->next==NULL){
return head;
}
ListNode * rest = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> st1, st2;
while(l1){
st1.push(l1->val);
l1 = l1->next;
}
while(l2){
st2.push(l2->val);
l2 = l2->next;
}
ListNode * dummy = new ListNode(-1);
ListNode * curr = dummy;
int carry = 0;
while(!st1.empty() && !st2.empty()){
int x = (carry + st1.top() + st2.top());
curr->next = new ListNode(x%10);
carry = x /10;
st1.pop();
st2.pop();
curr = curr->next;
}
while(!st1.empty()){
int x = (carry + st1.top());
curr->next = new ListNode(x%10);
carry = x/10;
st1.pop();
curr = curr->next;
}
while(!st2.empty()){
int x= carry + st2.top();
curr->next = new ListNode(x%10);
carry = x/10;
st2.pop();
curr = curr->next;
}
if(carry){
curr->next = new ListNode(carry);
}
curr = dummy->next;
curr = reverseList(curr);
return curr;
}
};