LeetCode 2. Add Two Numbers

Descripition:

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
Explanation: 342 + 465 = 807.


解法一:暴力(Accepted)
最简单的当然就是将两个链表转化成两个数字,然后分别相加,然后再将两个数的和转化成链表,需要注意两个数的和是0的情况,虽然这个方法时间复杂度并不高但是比较蠢,然后也 AC 了。

class Solution(object):
    # Approach #1 (Brute Force) [Accepted]
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        l1_cur = l1
        l1_res = 0
        i = 0
        while l1_cur is not None:
            l1_res += l1_cur.val * pow(10, i)
            l1_cur = l1_cur.next
            i += 1
        l2_cur = l2
        l2_res = 0
        i = 0
        while l2_cur is not None:
            l2_res += l2_cur.val * pow(10, i)
            l2_cur = l2_cur.next
            i += 1
        res = l1_res + l2_res

        remainder = res % 10
        res_head = ListNode(remainder)
        res_tail = res_head
        res /= 10
        while res != 0:
            remainder = res % 10
            res_node = ListNode(remainder)
            res_tail.next = res_node
            res_tail = res_node
            res /= 10

        return res_head

解法二:不转化成数,直接相加(Accepted)
由于这道题两个数的低数位都在链表的前方,因此我们其实没有必要转化成数然后相加,直接用一个变量保存进位,然后依次遍历两个链表,对应位相加就行了,这里唯一需要注意的就是两个链表长度可能不同,需要处理一下:

class Solution(object):
    # Approach #2 (One-pass l1 and l2) [Accepted]
    def addTwoNumbers(self, l1, l2):
        l1_cur = l1
        l2_cur = l2

        res_head = ListNode(-1)
        res_tail = res_head
        carry = 0
        while l1_cur is not None and l2_cur is not None:
            res_node_val = l1_cur.val + l2_cur.val + carry
            carry = 1 if res_node_val > 9 else 0
            res_node = ListNode(res_node_val % 10)
            res_tail.next = res_node
            res_tail = res_node
            l1_cur = l1_cur.next
            l2_cur = l2_cur.next

        l_cur = l2_cur if l1_cur is None else l1_cur
        while l_cur is not None or carry == 1:
            res_node_val = (0 if l_cur is None else l_cur.val) + carry
            carry = 1 if res_node_val > 9 else 0
            res_node = ListNode(res_node_val % 10)
            res_tail.next = res_node
            res_tail = res_node
            l_cur = None if l_cur is None else l_cur.next

        return res_head.next

这里要注意21行必须加括号,否则意思就会变成 res_node_val = 0 if l_cur is None else (l_cur.val + carry)。
但是楼主在评论区看到一种更简洁的写法,这里也分享出来:

class Solution(object):
        l1_cur = l1
        l2_cur = l2
        res_tail = res_head = ListNode(-1)
        carry = 0
        while l1_cur is not None or l2_cur is not None:
            l1_val = 0 if l1_cur is None else l1_cur.val
            l2_val = 0 if l2_cur is None else l2_cur.val
            res_node_val = l1_val + l2_val + carry
            carry = 1 if res_node_val > 9 else 0  # carry = res_node_val / 10
            res_tail.next = ListNode(res_node_val % 10)
            res_tail = res_tail.next
            l1_cur = None if l1_cur is None else l1_cur.next
            l2_cur = None if l2_cur is None else l2_cur.next
        if carry > 0:
            res_tail.next = ListNode(-1)
        return res_head.next
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值