单链表逆序相加
class Solution:
def addTwoNumbers(self, l1, l2):
root = None
pos = None
sum = 0
result = []
while sum > 0 or l1 != None or l2 != None:
v = 0
if l1 != None:
v = v + l1.val
l1 = l1.next
if l2 != None:
v = v + l2.val
l2 = l2.next
sum = sum + v
v = sum % 10
sum = int(sum/10)
node = ListNode(v)
if root == None:
root = node
if pos == None:
pos = node
else:
pos.next = node
pos = node
return root
网友评论