LSGO——LeetCode实战(链表系列):2题 两数相加(Add Two Numbers)

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

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

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

示例:

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

解法一:(暴力法)

程序一:程序思路比较全

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        h  = result = ListNode(0)
        num =0
        while l1 != None and l2 != None:
            num = l1.val + l2.val +num
            h.next = ListNode(num %10)
            num =num/10
            h = h.next
            l1 = l1.next
            l2 = l2.next
        if l2 != None:
            h.next = l2
            while h.next != None:
                num = h.next.val + num
                h.next.val = num%10
                h = h.next
                num = num /10
        elif l1!= None:
            h.next = l1
            while h.next != None:
                num = h.next.val + num
                h.next.val = num%10
                h = h.next
                num = num /10
        if num != 0:
            h.next=ListNode(num%10)
            if num/10 != 0:
                h =h.next
                h.next = ListNode(num/10)
        return result.next
       
                    
                

程序二:将程序一简化了一下

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        h  = result = ListNode(0)
        num =0
        while l1 != None or l2 != None:
            temp1 =l1.val if l1 != None else 0
            temp2 =l2.val if l2 != None else 0
            num = temp1 + temp2 +num
            h.next = ListNode(num %10)
            num =num/10
            h = h.next
            if l1 != None:
                l1 = l1.next
            if l2 != None:
                l2 = l2.next
        if num != 0:
            h.next=ListNode(num%10)
            if num/10 != 0:
                h =h.next
                h.next = ListNode(num/10)
        return result.next
                       

程序三:在速度上加快了很多,击败99%,内存消耗和第一个程序一样。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        newList = l1
        pre = ListNode(0)
        flage =0
        while l1 or l2:
            value = 0
            if l1 :
                pre =l1
                l1=l1.next
                value = pre.val+flage  
            else :
                pre.next=l2
                pre=l2
                value = flage
            if l2:
                value += l2.val
                l2 = l2.next 
            else:
                pre.next=l1
            pre.val = value % 10
            flage = 1 if value > 9 else 0
        if l1 == None and l2 == None and flage == 1:
            pre.next = ListNode(1)
        return newList

解法二:递归

这里的递归其实并不复杂,还是和 我以前的说的一样,我们去假设递归函数就是return结果的变量。速度属于中等。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def __init__(self):
        self.c = 0
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """

        if l1 == None and l2 ==None and self.c==0:
            return None
        if l1:
            self.c+=l1.val
            l1 =l1.next
        else:
            self.c+=0
        if l2:
            self.c+=l2.val
            l2 = l2.next
        else:
            self.c+=0
        cur = ListNode(self.c%10)
        self.c= self.c/10
        cur.next = self.addTwoNumbers(l1, l2)
        return cur
            

 

你可能感兴趣的:(LSGO——LeetCode实战(链表系列):2题 两数相加(Add Two Numbers))