思路:
1、反转链表(像这种单链表,要从尾部开始遍历的都想到先反转链表)
2、遍历累加,放入新的链表(同大数累加题目累加进位思路一致)
3、反转累加后的链表

public ListNode addInList (ListNode head1, ListNode head2) {
ListNode h1 = reverseList(head1);
ListNode h2 = reverseList(head2);
ListNode root = new ListNode(0);
ListNode result = root;
int carry = 0;
while(h1 != null || h2 != null){
int s1 = h1==null?0:h1.val;
int s2 = h2==null?0:h2.val;
if(h1 != null){
h1 = h1.next;
}
if(h2 != null){
h2 = h2.next;
}
int sum = s1 + s2 + carry;
carry = sum / 10;
root.val = sum%10;
if(h1 != null || h2 != null){
root.next = new ListNode(0);
root = root.next;
}
}
if(carry != 0){
root.next = new ListNode(0);
root = root.next;
root.val = carry;
}
return reverseList(result);
}
ListNode reverseList(ListNode root){
ListNode result = null;
while(root != null){
ListNode temp = root;
root = root.next;
temp.next = result;
result = temp;
}
return result;
}
}