leetcode刷题(2)

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

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

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

示例:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
 

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

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        len_l1 = 1
        len_l2 = 1
        p1 = l1
        p2 = l2
        result = []
        get_in = 0
        while True:
            if (p1.next == None) and (p2.next ==  None):
                break
            
            if (p1.next != None):
                p1 = p1.next
                len_l1 += 1
            
            if (p2.next != None):
                p2 = p2.next
                len_l2 += 1
        
        if len_l1 == len_l2 :
            p1 = l1
            p2 = l2

            for _ in  range(len_l1):
                result.append(p1.val + p2.val)
                p1 = p1.next
                p2 = p2.next

            for i in range(len(result)):
                if result[i] < 10:
                    pass
                else:
                    m = result[i] % 10
                    result[i] = m
                    if (i + 1) == len(result):
                        result.append(1)
                    else:
                        result[i+1] = result[i+1] + 1
            p2 = ListNode(result[0])
            p1 = p2
            for i in range(1, len(result)):
                p1.next = ListNode(result[i])
                p1 = p1.next
            return p2 
        else:
            if len_l1 > len_l2 :
                length = len_l1 - len_l2
                p2 = l2
                for _ in range(len_l2-1):
                    p2 = p2.next
                print(p2)
                for _ in range(length):
                    p2.next = ListNode(0)
                    p2 = p2.next
            else:
                length = len_l2 - len_l1
                p1 = l1
                for _ in range(len_l1-1):
                    p1 = p1.next
                print(p1)
                for _ in range(length):
                    p1.next = ListNode(0)
                    p1 = p1.next

            p1 = l1
            p2 = l2
            
            if len_l1 > len_l2:
                pass
            else:
                len_l1 = len_l2

            for _ in  range(len_l1):
                result.append(p1.val + p2.val)
                p1 = p1.next
                p2 = p2.next
            
            for i in range(len(result)):
                if result[i] < 10:
                    pass
                else:
                    m = result[i] % 10
                    result[i] = m
                    if (i + 1) == len(result):
                        result.append(1)
                    else:
                        result[i+1] = result[i+1] + 1

            p2 = ListNode(result[0])
            p1 = p2
            for i in range(1, len(result)):
                p1.next = ListNode(result[i])
                p1 = p1.next
            return p2 

 

我的代码不是很好,只做一个记录,在写代码前建议打印出数据格式看一下

你可能感兴趣的:(leetcode)