public ListNode addTwoNumbers(ListNode l1,ListNode l2){
Stack<Integer> stack1 = buildStack(l1);
Stack<Integer> stack2 = buildStack(l2);
ListNode head = new ListNode();
int carry = 0;
while(!stack1.isEmpty() || !stack2.isEmpty() || carry!=0){
int x = stack1.isEmpty()?0:stack1.pop();
int y = stack2.isEmpty()?0:stack2.pop();
int sum = x + y + carry;
ListNode node = new ListNode();
node.next = head.next;
head.next = node;
carry = sum/10;
}
return head.next;
}
private Stack<Integer> buildStack(ListNode l){
Stack<Integer> stack = new Stack<>();
while(l!=null){
stack.push(l.val);
l = l.next;
}
return stack;
}