-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2num.py
More file actions
24 lines (23 loc) · 694 Bytes
/
a2num.py
File metadata and controls
24 lines (23 loc) · 694 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
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
current = dummy
carry = 0
while l1 or l2:
x = l1.val if l1 else 0
y = l2.val if l2 else 0
total = carry + x + y
carry = total // 10
current.next = ListNode(total % 10)
current = current.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry > 0:
current.next = ListNode(carry)
return dummy.next