-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
247 lines (183 loc) · 6.84 KB
/
AddTwoNumbers.java
File metadata and controls
247 lines (183 loc) · 6.84 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package Algorithms.LinkedListAlgos;
import java.math.BigDecimal;
/**
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
* l1 and l2 are already in reverse order just like we need to add two numbers and carry forward the digit if necessary
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 11 Nov 2024
* @link 2. Add Two Numbers <a href="https://leetcode.com/problems/add-two-numbers/">LeetCode link</a>
* @topics Linked List, Math, Recursion
* @companies Amazon, Meta, Bloomberg, Microsoft, Nvidia, Goldman, Apple, Oracle, ByteDance, Yandex, Capgemini, Adobe, Uber, Yahoo, Avito, TikTok, tcs, Accenture, josh, Infosys, Cisco
*/
public class AddTwoNumbers {
public static class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public static void main(String[] args) {
ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
// ListNode l1 = new ListNode(9);
// ListNode l2 = new ListNode(1, new ListNode(9, new ListNode(9)));
ListNode l3 = addTwoNumbers(l1, l2);
for(ListNode trav=l3; trav!=null; trav=trav.next){
System.out.print(trav.val + " ");
}
}
/**
1
999
+ 223
----
1222
----
*/
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1), trav = dummy;
int carryForward = 0;
while(l1 != null || l2 != null) {
int val1 = 0, val2 = 0;
if(l1 != null) {
val1 = l1.val;
l1 = l1.next;
}
if(l2 != null) {
val2 = l2.val;
l2 = l2.next;
}
int sum = carryForward + val1 + val2;
carryForward = sum/10;
int currNum = sum % 10;
trav.next = new ListNode(currNum);
trav = trav.next;
}
if(carryForward > 0) {
trav.next = new ListNode(carryForward);
}
return dummy.next;
}
public static ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0), curr = dummy;
int carry = 0;
while(l1!=null || l2!=null || carry==1){
int sum = 0;
if(l1!=null){
sum+=l1.val;
l1=l1.next;
}
if(l2!=null){
sum+=l2.val;
l2=l2.next;
}
sum+=carry;
carry = sum/10;
ListNode node = new ListNode(sum%10);
curr.next = node;
curr = node;
}
return dummy.next;
}
/**
*
*/
public static ListNode addTwoNumbersMyApproachOld(ListNode l1, ListNode l2) {
ListNode l3 = new ListNode();
ListNode trav = l3;
int carryForward = 0;
while(l1!=null || l2!=null) {
int n1 = 0;
int n2 = 0;
if(l1 != null) {
System.out.println("l1: " + l1.val);
n1 = l1.val;
l1 = l1.next;
}
if(l2 != null) {
System.out.println("l2: "+l2.val);
n2 = l2.val;
l2 = l2.next;
}
int n3 = n1+n2+carryForward;
// if (n3 > 9) { ---- OPTIONAL as n3 = 5%10 = 5 and carryForward =5/10 = 0 -- int not double
// carryForward = n3/10;
// n3 = n3 % 10;
// } else {
// carryForward = 0;
// }
carryForward = n3/10;
n3 = n3 % 10;
trav.val = n3;
trav.next = new ListNode(); // ----> if we use "ListNode dummy = new ListNode(-1), trav = dummy;" --> no need to worry about --> trav.next = new ListNode(); trav.next.val = carryForward; trav.next = null;
if (l1 == null && l2 == null){ // to carryForward the last digit sum if > 9
if(carryForward !=0)
trav.next.val = carryForward;
else
trav.next = null; // because of above trav.next = new ListNode(); condition
} else {
trav = trav.next;
}
}
return l3;
}
public ListNode addTwoNumbersUsingBigDecimal(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1), trav = dummy;
ListNode trav1 = l1, trav2 = l2;
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
while(trav1 != null) {
sb1.append(trav1.val);
trav1 = trav1.next;
}
while(trav2 != null) {
sb2.append(trav2.val);
trav2 = trav2.next;
}
BigDecimal sum = new BigDecimal(sb1.reverse().toString()).add(new BigDecimal(sb2.reverse().toString()));
String reverseSum = new StringBuilder().append(sum).reverse().toString();
for(String s: reverseSum.split("")){
trav.next = new ListNode(Integer.parseInt(s));
trav = trav.next;
}
return dummy.next;
}
/**
* Working but failing with java.lang or java.lang.NumberFormatException when
* l1=[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1] and l2=[5,6,4]
* i.e when total sequence is greater than Integer.MAX_VALUE and Long.MAX_VALUE
* So, use strings and solve it like an elementary school addition i.e carry forward the digit to left side
*/
public static ListNode addTwoNumbersUsingLongNotWorking(ListNode l1, ListNode l2) {
int n1 = 0;
int n2 = 0;
int n3 = 0;
ListNode l3 = new ListNode();
String temp = "";
for(ListNode trav=l1; trav!=null; trav=trav.next){
temp = trav.val + temp; // reverse
}
n1 = Integer.parseInt(temp);
System.out.println(n1);
temp = "";
for(ListNode trav=l2; trav!=null; trav=trav.next){
temp = trav.val + temp; // reverse
}
n2 = Integer.parseInt(temp);
System.out.println(n2);
n3 = n1 + n2;
temp = "" + n3;
temp = new StringBuilder(temp).reverse().toString();
System.out.println(temp);
temp = temp.replaceAll("0", "");
String[] l3Arr = temp.split("");
ListNode trav=l3;
for (int i=0; i<l3Arr.length; i++, trav=trav.next){
trav.val =Integer.parseInt(l3Arr[i]);
if(i<l3Arr.length-1) trav.next = new ListNode();
}
return l3;
}
}