# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defaddTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: l1_nums, l2_nums = [], [] while l1: l1_nums.append(l1.val) l1 = l1.next
while l2: l2_nums.append(l2.val) l2 = l2.next
l1_nums = l1_nums[::-1] l2_nums = l2_nums[::-1] n = int(''.join(map(str, l1_nums))) + int(''.join(map(str, l2_nums))) head = ListNode(n%10) current = head n = n // 10
while n: val = n % 10 new_node = ListNode(val) current.next = new_node current = current.next n = n // 10 return head