LeetCode:2. Add Two Numbers

原文链接

Question

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:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

My Solution

Notice: non-empty list, non-negative integers and stored in reverse order.

Original Solution(Not passed)

Train of thought:

Construct a new linked list to store the sum. For the extra time of instantiating each node, the solution gets Time Limit Exceeded warning.

class MySolution1(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        head = ListNode(0)
        head.next = ListNode(0)
        result = head.next

        while l1 is not None:
            if l2 is not None:
                the_sum = l1.val + l2.val
                if the_sum / 10 != 1:
                    result.val += the_sum
                    l1 = l1.next
                    l2 = l2.next
                    if l1 is not None or l2 is not None:
                        result.next = ListNode(0)
                        result = result.next
                else:
                    result.val += the_sum % 10
                    l1 = l1.next
                    l2 = l2.next
                    result.next = ListNode(1)
                    result = result.next

            else:
                the_sum = l1.val + result.val
                if the_sum != 10:
                    result.val = the_sum
                    l1 = l1.next
                    if l1 is not None:
                        result.next = ListNode(0)
                        result = result.next

                else:
                    result.val = 0
                    result.next = ListNode(1)
                    result = result.next

        while l2 is not None:
            the_sum = l2.val + result.val
            if the_sum != 10:
                result.val = the_sum
                l2 = l2.next
                if l2 is not None:
                    result.next = ListNode(0)
                    result = result.next

            else:
                result.val = 0
                result.next = ListNode(1)
                result = result.next
                l2 = l2.next

        head = head.next

        return head

Optimized Solution

Train of thought:

The node is useless after the digit is processed. Based on the idea, we can save more time and space, which means we need to instantiate at most one node(to store the potential carry bit).

class MySolution2(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        carry = 0
        head = l1
        previous = head

        while l1 is not None and l2 is not None:
            carry, l1.val = divmod(l1.val + l2.val + carry, 10)
            previous = l1
            l1 = l1.next
            l2 = l2.next

        if l1 is None and l2 is None:
            if carry == 1:
                previous.next = ListNode(1)
            return head

        while l1 is not None:
            carry, l1.val = divmod(l1.val + carry, 10)
            if l1.next is not None:
                l1 = l1.next

            elif carry == 1:
                l1.next = ListNode(1)
                break

            else:
                break

        if l2 is not None:
            previous.next = l2
            while l2 is not None:
                carry, l2.val = divmod(l2.val + carry, 10)
                if l2.next is not None:
                    l2 = l2.next

                elif carry == 1:
                    l2.next = ListNode(1)
                    break

                else:
                    break

        return head

Editorial Solution of LeetCode

Train of thought:

The idea is just like the first solution of mine. However, the second one is more time-saving and space-saving.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值