#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head1 ListNode类 
# @param head2 ListNode类 
# @return ListNode类
#
class Solution:
    def addInList(self , head1: ListNode, head2: ListNode) -> ListNode:
        a,b = [],[]
        i1,i2 = 0,0
        while head1 or head2:
            if head1!=None:
                i1=i1+1
                a.append(head1.val)
                head1 = head1.next
            if head2!=None:
                i2=i2+1
                b.append(head2.val)
                head2 = head2.next
        #i1_1,i2_1 = i1,i2
        a_value,b_value =0,0
        max_i = max(i1,i2)
        for a1 in a:
            a_value = a_value + a1*(10**(i1-1))
            i1 = i1-1
        for b1 in b:
            b_value = b_value + b1*(10**(i2-1))
            i2= i2 -1
        num = a_value + b_value
        if num//10**(max_i-1)>=10:
            num_i =max_i+1
        else:
            num_i =max_i
        pre=head = ListNode(0)
        while num_i:
            temp = num//10**(num_i-1)
            temp_node = ListNode(-1)
            head.next = temp_node
            temp_node.val=temp
            head = head.next
            if num!=0:
                num = num - temp*10**(num_i-1)
            num_i=num_i-1
        return pre.next