给定两个非空链表逆序存储的的非负整数,每个节点只存储一位数组。
请你把两个链表相加以下相同方法返回链表,保证两个数都不会以 0 开头。
数据范围: ,每个节点的值都满足
ListNode res = new ListNode(-1); ListNode cur = res; int carry = 0; while(head1 != null || head2 != null || carry != 0){ int i = head1 == null ? 0 : head1.val; int j = head2 == null ? 0 : head2.val; int sum = i + j + carry; carry = sum / 10; ListNode tem = new ListNode(sum % 10); cur.next = tem; cur = cur.next; head1 = head1 == null ? null : head1.next; head2 = head2 == null ? null : head2.next; } return res.next;