这是我代码的显示结果
解答失败:
测试用例:[9,9,9,9,9,9,9]
[9,9,9,9]
测试结果:[8,9,9,9,0,0,0]
期望结果:[8,9,9,9,0,0,0,1]
这是具体代码:
class Solution {
ListNode res = null, cur = null;
int c = 0;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 != null || l2 != null) {
int a = l1 != null ? l1.val + c : c; //l1当前结点的值与进位的和
int b = l2 != null ? l2.val : 0; //l2当前结点的值
int sum = (a + b) % 10; //新链表当前节点的值
c = (a + b) / 10; //进位值
if (cur == null & res == null) {
res = cur = new ListNode(sum);
} else {
cur.next = new ListNode(sum);
cur = cur.next;
}
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
if (l1 == null & l2 == null & c == 0) {
return res;
}
addTwoNumbers(l1, l2);
}
return res;
}
}