-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_AddTwoNumers.py
32 lines (31 loc) · 915 Bytes
/
2_AddTwoNumers.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def addTwoNumbers(self, l1, l2):
result = current = ListNode(0)
carry = 0
while True:
value = carry + l1.val + l2.val
carry = value / 10
current.val = value % 10
if (not l1.next) and (not l2.next):
if carry > 0:
current.next = ListNode(carry)
break
if l1.next:
l1 = l1.next
else:
l1 = ListNode(0)
if l2.next:
l2 = l2.next
else:
l2 = ListNode(0)
current.next = ListNode(carry)
current = current.next
return result