-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path002-AddTwoNumbers.py
37 lines (33 loc) Β· 997 Bytes
/
002-AddTwoNumbers.py
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
29
30
31
32
33
34
35
36
37
# 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]:
# Reverse int L1
val1 = ""
curr1 = l1
while curr1:
temp = val1
val1 = str(curr1.val) + temp
curr1 = curr1.next
# Reverse int L2
val2 = ""
curr2 = l2
while curr2:
temp = val2
val2 = str(curr2.val) + temp
curr2 = curr2.next
# jumlahkan int L1 + int L2 dan buat listNode baru
result = str(int(val1) + int(val2))
length = len(result)
newList = ListNode()
temp = newList
while length > 0:
temp.next = ListNode(int(result[length - 1]), None)
temp = temp.next
length -= 1
return newList.next