Problem 1 and 2
This commit is contained in:
22
0002-add-two-numbers/solution.py
Normal file
22
0002-add-two-numbers/solution.py
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user