题目
445. 两数相加 II
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:
输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 8 -> 0 -> 7
递归解法 先给长度短的补上0
int carry = 0;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1==null)
return l2;
if (l2==null)
return l1;
int length1 = 0;
int length2 = 0;
ListNode node = l1;
while (node!=null){
length1++;
node = node.next;
}
node = l2;
while (node!=null){
length2++;
node = node.next;
}
ListNode out ;
if (length1!=length2){
ListNode listNode = new ListNode(0);
ListNode go =listNode;
int count = Math.abs(length1-length2)-1;
while (count>0){
listNode.next = new ListNode(0);
listNode = listNode.next;
count--;
}
listNode.next = length1>length2?l2:l1;
if (length1>length2){
out = add(l1,go);
}else {
out = add(go,l2);
}
}else
out = add(l1,l2);
if (carry!=0){
ListNode carryNode = new ListNode(carry);
carryNode.next = out;
out = carryNode;
}
return out;
}
public ListNode add(ListNode l1, ListNode l2){
int val;
if (l1.next==null&&l2.next==null){
val = l1.val + l2.val;
}else {
addTwoNumbers(l1.next,l2.next);
val = l1.val + l2.val + carry;
}
carry = val / 10;
val = carry==0?val:val-10;
l1.val = val;
return l1;
}
迭代做法 利用栈迭代 当栈无数据时用0代替
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1==null)
return l2;
if (l2==null)
return l1;
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
int carry = 0;
while (l1!=null){
stack1.add(l1.val);
l1 = l1.next;
}
while (l2!=null){
stack2.add(l2.val);
l2 = l2.next;
}
ListNode out = null;
while (!(stack1.isEmpty()&&stack2.isEmpty())){
int n1 = stack1.isEmpty()?0:stack1.pop();
int n2 = stack2.isEmpty()?0:stack2.pop();
int sum = n1 + n2 + carry;
carry = sum / 10;
if (sum>9){
sum = sum - 10;
}
ListNode node = new ListNode(sum);
node.next = out;
out = node;
}
if (carry!=0){
ListNode carryNode = new ListNode(carry);
carryNode.next = out;
out = carryNode;
}
return out;
}