class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int j=0;
two.ListNode listNode = new two.ListNode(0);
two.ListNode listNode1 = listNode;
while (l1 != null || l2 != null){
int x = l1 ==null ? 0 : l1.val;
int y = l2 ==null ? 0 : l2.val;
int n = x + y + j;
if (n > 9){
listNode1.next = new two.ListNode(n -10);
listNode1 = listNode1.next;
j = 1;
}else {
listNode1.next = new two.ListNode(n);
listNode1 = listNode1.next;
j = 0;
}
if (l1 != null){
l1 = l1.next;
}
if (l2 != null){
l2 = l2.next;
}
}
if (j == 1){
listNode1.next = new two.ListNode(j);
}
return listNode.next;
}
}
leetcode第2题 两数相加 java
最新推荐文章于 2024-08-12 16:47:32 发布