forked from Nidhi-Sharma9419/Elite-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
49 lines (42 loc) · 1.29 KB
/
AddTwoNumbers.java
File metadata and controls
49 lines (42 loc) · 1.29 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
//Brute Force
//Time Complexity
import java.math.BigInteger;
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> l1s = new Stack<>();
while(l1 != null){
l1s.push(l1.val);
l1 = l1.next;
}
String l1str ="";
while(!l1s.isEmpty()){
l1str += l1s.pop();
}
BigInteger l1int = new BigInteger(l1str);
Stack<Integer> l2s = new Stack<>();
while(l2 != null){
l2s.push(l2.val);
l2 = l2.next;
}
String l2str ="";
while(!l2s.isEmpty()){
l2str += l2s.pop();
}
BigInteger l2int = new BigInteger(l2str);
BigInteger sum = l1int.add(l2int);
String sumStr = String.valueOf(sum);
ListNode ans = new ListNode();
ListNode tmp = ans;
for(int i = sumStr.length()-1 ; i>=0 ; i--){
tmp.val = Integer.parseInt(String.valueOf(sumStr.charAt(i)));
if( i != 0){
tmp.next = new ListNode();
}else{
// for last element , set next null;
tmp.next = null;
}
tmp = tmp.next;
}
return ans;
}
}