Code: Select all
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1:ListNode, 12: ListNode) --> ListNode:
dummy = ListNode(0)
current = dummy
carry = 0
while l1 or 12 or carry:
val1 = l1.val if l1 else 0
val2 = l2.val if 12 else 0
total = val1 + val2 + carry
carry = total // 10
digit = total % 10
current.next = ListNode(digit)
current = current.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
return dummy.next
Mobile version