import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
LinkedList<ListNode> stack1 = new LinkedList<>();
LinkedList<ListNode> stack2 = new LinkedList<>();
ListNode cur = head1;
while(cur!=null){
stack1.add(cur);
cur = cur.next;
}
cur = head2;
while(cur!=null){
stack2.add(cur);
cur = cur.next;
}
int plus = 0;
ListNode head = new ListNode(0);
while(!stack1.isEmpty()|| !stack2.isEmpty()){
int i1 = 0;
int i2 = 0;
if(!stack1.isEmpty()){
i1 = stack1.removeLast().val;
}
if(!stack2.isEmpty()){
i2 = stack2.removeLast().val;
}
int value = i1+i2+plus;
if(value>9){
plus = value/10;
value = value%10;
}else{
plus = 0;
}
ListNode node = new ListNode(value);
System.out.println(value);
node.next = head.next;
head.next = node;
}
if(plus>0){
head.val = plus;
return head;
}else{
return head.next;
}
}
}