Problem 1 and 2

This commit is contained in:
Simon Oberzier
2026-01-08 21:34:54 +01:00
parent 38460afca7
commit 293fee1fc5
2 changed files with 31 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
i = 1
n1 = n2 = 0
while not (l1 == None and l2 == None):
if l1 != None:
n1 += (l1.val * i)
l1 = l1.next
if l2 != None:
n2 += (l2.val * i)
l2 = l2.next
i *= 10
n = None
for c in list(str(n1 + n2)):
n = ListNode(int(c), n)
return n