-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
38 lines (26 loc) · 791 Bytes
/
test.py
File metadata and controls
38 lines (26 loc) · 791 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
29
30
31
32
33
34
35
36
37
38
class Solution:
def addTwoNumbers(self, l1, l2):
dummy = self.ListNode(0)
current = dummy
carry = 0
while l1 or l2 or carry:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
total = val1 + val2 + carry
carry = total // 10
current.next = self.ListNode(total % 10)
current = current.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
return dummy.next
solution = Solution()
result = solution.addTwoNumbers([2], [2])
def print_linked_list(node):
while node:
print(node.val, end=" -> " if node.next else "")
node = node.next
print()
print("Result linked list:")
print_linked_list(result)