目前我每天都在做Leetcode streak,我不知道为什么我的代码无法通过案例3。(我已经完成了修改和跟踪代码,但也许我有点笨,无法通过)没有发现错误。)希望这不是任何愚蠢的傻瓜错误,感谢您的帮助!
参考这个leetcode问题。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {
}
* ListNode(int val) {
this.val = val;
}
* ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Making an empty LinkList to store answers
ListNode answer = new ListNode();
// A pointer the transverse the answer
ListNode current = answer;
// Dont forget carries
int carry = 0;
// Setting up global variable
int l1_value, l2_value;
// If carry = 0 means Answer is DONE!
while(l1 != null || l2 != null || carry != 0) {
// If l1 -> null, then 0
if (l1 != null) {
l1_value = l1.val; // Directly get val if it is public
} else l1_value = 0;
if (l2 != null) {
l2_value = l2.val;
} else l2_value = 0;
// DONT FORGET ABOUT CARRY
int total = l1_value + l2_value + carry;
int remainder = total % 10;
current.next = new ListNode(remainder, current.next);
carry = total / 10;
// Transverse to next, else null
if (l1 != null && l1.next != null) {
l1 = l1.next;
} else l1 = null;
// Error occurs because at some point this would become NULL, causes the NullPointerException
if (l2 != null && l2.next != null) {
l2 = l2.next;
} else l2 = null;
// I think the extra 0s, 1s are from the CARRY here [Added if condition]
// Error still occur but much better, added [carry != 0], T1/2 done! but T3 zzzz
if (l1 == null && l2 == null && carry != 0) {
current.next = new ListNode(carry, current.next);
}
current = current.next;
}
return answer.next;
}
}
问题与解答:
l1 = [9,9,9,9,9,9,9]
l2 = [9,9,9,9]
预期输出 = [8,9,9,9,0,0,0,1]
我的输出= [8,9,9,9,0,0,1,1,0]
你基本上已经自己解决了。问题正是您所期望的:
// I think the extra 0s, 1s are from the CARRY here [Added if condition]
// Error still occur but much better, added [carry != 0], T1/2 done! but T3 zzzz
if (l1 == null && l2 == null && carry != 0) {
current.next = new ListNode(carry, current.next);
}
这只是多余的。您之前已经添加了进位:
int total = l1_value + l2_value + carry;
所以如果你有进位,总计总是 != 0。因此余数是 != 0 因此,这个 ListNode 就足够了:
current.next = new ListNode(remainder, current.next);
只需删除ListNode的额外创建即可,结果将如预期。
我的完整代码:
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// Making an empty LinkList to store answers
ListNode answer = new ListNode();
// A pointer the transverse the answer
ListNode current = answer;
// Dont forget carries
int carry = 0;
// Setting up global variable
int l1_value, l2_value;
// If carry = 0 means Answer is DONE!
while (l1 != null || l2 != null || carry != 0) {
// If l1 -> null, then 0
if (l1 != null) {
l1_value = l1.val; // Directly get val if it is public
} else l1_value = 0;
if (l2 != null) {
l2_value = l2.val;
} else l2_value = 0;
// DONT FORGET ABOUT CARRY
int total = l1_value + l2_value + carry;
int remainder = total % 10;
current.next = new ListNode(remainder, current.next);
carry = total / 10;
// Transverse to next, else null
if (l1 != null && l1.next != null) {
l1 = l1.next;
} else l1 = null;
// Error occurs because at some point this would become NULL, causes the NullPointerException
if (l2 != null && l2.next != null) {
l2 = l2.next;
} else l2 = null;
current = current.next;
}
return answer.next;
}
public static void main(String[] args) {
var l1 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
var l2 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
ListNode result = new Solution().addTwoNumbers(l1, l2);
print(result);
}
private static void print(ListNode result) {
System.out.print(result.val);
if (result.next != null) {
print(result.next);
}
}
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}