Leetcode打卡(二)Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

 

Solution

Algorithm

两个0~9之间的数字相加的和可能位于0~18之间,和可能是两位数也可能是一位数,因此设计一个carry用来存储“溢出”的一位数字。carry要么是0要么是1。

  1. 初始化dummy head
  2. 初始化carry为0
  3. 初始化p和q分别为l1和l2的头
  4. 循环,直到l1和l2都到了end
    1. 如果p没有到达l1的end,那么x = p , 否则x = 0
    2. 如果q没有到达l2的end,那么x = p , 否则x = 0
    3. sum = p + q + carry
    4. 更新 carry = sum/10
    5. 在当前节点后建立新的节点,值为 sum mod 10,节点后移一个
    6. 后移p和q
  5. 如果carry = 1, 在最后添加一个新的节点
  6. 返回dummy head的下一个节点

Just like how you would sum two numbers on a piece of paper, we begin by summing the least-significant digits, which is the head of l1l1 and l2l2. Since each digit is in the range of 0…9, 0…9, summing two digits may "overflow". For example 5+7=125+7=12. In this case, we set the current digit to 22 and bring over the carry=1carry=1 to the next iteration. carrycarry must be either 00 or 11 because the largest possible sum of two digits (including the carry) is 9+9+1=199+9+1=19.

The pseudocode is as following:

  • Initialize current node to dummy head of the returning list.
  • Initialize carry to 00.
  • Initialize pp and qq to head of l1l1 and l2l2 respectively.
  • Loop through lists l1l1 and l2l2 until you reach both ends.
    • Set xx to node pp's value. If pp has reached the end of l1l1, set to 00.
    • Set yy to node qq's value. If qq has reached the end of l2l2, set to 00.
    • Set sum=x+y+carrysum=x+y+carry.
    • Update carry=sum/10carry=sum/10.
    • Create a new node with the digit value of (summod10)(summod10) and set it to current node's next, then advance current node to next.
    • Advance both pp and qq.
  • Check if carry=1carry=1, if so append a new node with digit 11 to the returning list.
  • Return dummy head's next node.

Note that we use a dummy head to simplify the code. Without a dummy head, you would have to write extra conditional statements to initialize the head's value.

 

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyHead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyHead;
    int carry = 0;
    while (p != null || q != null) {
        int x = (p != null) ? p.val : 0;
        int y = (q != null) ? q.val : 0;
        int sum = carry + x + y;
        carry = sum / 10;
        curr.next = new ListNode(sum % 10);
        curr = curr.next;
        if (p != null) p = p.next;
        if (q != null) q = q.next;
    }
    if (carry > 0) {
        curr.next = new ListNode(carry);
    }
    return dummyHead.next;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值