https://leetcode-cn.com/problems/add-two-numbers/

题目

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

思路

题目中加粗关键字注意下:

  • 非空
  • 逆序
  • 一位

解释:
链表从左往右依次是所待变数字的低位到高位, 2 -> 4 -> 3 代表342, 5 -> 6 -> 4 代表465,所以没有必要将链表转成数字再进行相加,也没法那样做,应为如果链表相当之长,会超过整形的范围,所以就从低位往高位挨个算就行了。
针对单次计算x+y=zx+y=zx+y=z,只需要考虑是否向前进位的问题即可。

第一次提交

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
              ListNode result = new ListNode(0);
        ListNode current1 = l1, current2 = l2, currentResult = result;
        int carry = 0;
        while (current1 != null && current2 != null) {
            int val = (current1.val + current2.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current1.val + current2.val + carry) / 10;
            current1 = current1.next;
            current2 = current2.next;
            currentResult = currentResult.next;
        }
        while (current1 != null) {
            int val = (current1.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current1.val + carry) / 10;
            current1 = current1.next;
            currentResult = currentResult.next;
        }
        while (current2 != null) {
            int val = (current2.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current2.val + carry) / 10;
            current2 = current2.next;
            currentResult = currentResult.next;
        }
        return result.next;
    }
}

第一次提交未通过

测试用例 我的结果 预期结果
[5],[5] [0] [0,1]

忽略了上述这种,结果位数比加数位数多且存在进位的情况

第二次提交

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
              ListNode result = new ListNode(0);
        ListNode current1 = l1, current2 = l2, currentResult = result;
        int carry = 0;
        while (current1 != null && current2 != null) {
            int val = (current1.val + current2.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current1.val + current2.val + carry) / 10;
            current1 = current1.next;
            current2 = current2.next;
            currentResult = currentResult.next;
        }
        while (current1 != null) {
            int val = (current1.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current1.val + carry) / 10;
            current1 = current1.next;
            currentResult = currentResult.next;
        }
        while (current2 != null) {
            int val = (current2.val + carry) % 10;
            currentResult.next = new ListNode(val);
            carry = (current2.val + carry) / 10;
            current2 = current2.next;
            currentResult = currentResult.next;
        }
        if(carry!=0) {
            currentResult.next = new ListNode(carry);
        }
        return result.next;
    }
}

总结

  1. 充足的测试用例很重要;
  2. 编写代码的时候,边界条件,特殊情况一定得考虑充分。

results matching ""

    No results matching ""