-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path445-AddTwoNumbersList.java
61 lines (55 loc) · 1.45 KB
/
445-AddTwoNumbersList.java
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> s1= new Stack<>();
Stack<Integer> s2= new Stack<>();
ListNode l1_temp=l1;
ListNode l2_temp=l2;
while(l1_temp!=null){
s1.push(l1_temp.val);
l1_temp=l1_temp.next;
}
while(l2_temp!=null){
s2.push(l2_temp.val);
l2_temp=l2_temp.next;
}
Stack<Integer> s3=new Stack<>();
int carry=0;
while(!s1.isEmpty() || !s2.isEmpty()){
int result=0;
if(!s1.isEmpty())
result=result+s1.pop();
if(!s2.isEmpty())
result=result+s2.pop();
result = result + carry;
if(result>9)
carry = 1;
else
carry = 0;
s3.push(result%10);
}
if(carry!=0)
s3.push(carry);
ListNode ans=null;
ListNode prev=null;
while(!s3.isEmpty()){
ListNode n = new ListNode(s3.pop());
if(ans==null){
ans=n;
prev=ans;
}
else{
prev.next = n;
prev = n;
}
}
return ans;
}
}