-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode445_AddTwoNumbers.java
More file actions
84 lines (67 loc) · 2.24 KB
/
Copy pathleetcode445_AddTwoNumbers.java
File metadata and controls
84 lines (67 loc) · 2.24 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
import java.util.Stack;
public class leetcode445_AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
Stack<Integer> result = new Stack<>();
int num1,num2,sum=0, carry=0;
while(l1 != null){
stack1.add(l1.val);
l1 = l1.next;
}
while(l2 != null){
stack2.add(l2.val);
l2 = l2.next;
}
while(!stack1.isEmpty() && !stack2.isEmpty()){
sum = carry + stack1.pop() + stack2.pop();
if(sum /10 > 0)
carry = 1;
else
carry = 0;
//finalList.next = new ListNode(sum%10);
//finalList = finalList.next;
result.add(sum%10);
}
while(!stack1.isEmpty()){
sum = carry + stack1.pop();
if(sum /10 > 0)
carry = 1;
else
carry = 0;
//finalList.next = new ListNode(sum %10 );
//finalList = finalList.next;
result.add(sum%10);
}
while(!stack2.isEmpty()){
sum = carry + stack2.pop();
if(sum /10 > 0)
carry = 1;
else
carry = 0;
//finalList.next = new ListNode(sum %10 );
//finalList = finalList.next;
result.add(sum%10);
}
if(carry == 1)
result.add(1);
ListNode finalList = new ListNode(result.pop());
ListNode finalListCopy = finalList;
while(!result.isEmpty()){
finalList.next = new ListNode(result.pop());
finalList = finalList.next;
}
return finalListCopy;
}
public static void main(String[] args) {
ListNode list1 = new ListNode(5);
/*list1.next = new ListNode(9);
list1.next.next = new ListNode(9);
list1.next.next.next = new ListNode(9);*/
ListNode list2 = new ListNode(5);
/*list2.next = new ListNode(6);
list2.next.next = new ListNode(4);*/
leetcode445_AddTwoNumbers obj = new leetcode445_AddTwoNumbers();
ListNode res = obj.addTwoNumbers(list1,list2);
}
}