Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- # @param {ListNode} l1
- # @param {ListNode} l2
- # @return {ListNode}
- def addTwoNumbers(self, l1, l2):
- def listiter(l):
- cur = l
- while cur:
- yield cur.val
- cur = cur.next
- def list2num(l):
- s, coe = 0, 1
- for x in listiter(l):
- s += coe * x
- coe *= 10
- return s
- def num2list(n):
- x, n = n % 10, n // 10
- head = ListNode(x)
- cur = head
- while n > 0:
- x, n = n % 10, n // 10
- newNode = ListNode(x)
- cur.next = newNode
- cur = newNode
- return head
- n1, n2 = list2num(l1), list2num(l2)
- s = n1 + n2
- return num2list(s)
Advertisement
Add Comment
Please, Sign In to add comment