-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddTwoNumbers.py
More file actions
28 lines (26 loc) · 811 Bytes
/
addTwoNumbers.py
File metadata and controls
28 lines (26 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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]:
curr = l1
num1 = ''
while curr:
num1 += str(curr.val)
curr = curr.next
curr2 = l2
num2 = ''
while curr2:
num2 += str(curr2.val)
curr2 = curr2.next
sum_ = int(num1[::-1]) + int(num2[::-1])
sum_ = str(sum_)[::-1]
head = ListNode(sum_[0])
prev = head
for i in range(1,len(sum_)):
curr = ListNode(sum_[i])
prev.next = curr
prev = curr
return head