bigeagle

Untitled

Aug 18th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1. class Solution:
  2.     # @param {ListNode} l1
  3.     # @param {ListNode} l2
  4.     # @return {ListNode}
  5.     def addTwoNumbers(self, l1, l2):
  6.         def listiter(l):
  7.             cur = l
  8.             while cur:
  9.                 yield cur.val
  10.                 cur = cur.next
  11.         def list2num(l):
  12.             s, coe = 0, 1
  13.             for x in listiter(l):
  14.                 s += coe * x
  15.                 coe *= 10
  16.             return s
  17.         def num2list(n):
  18.             x, n = n % 10, n // 10
  19.             head = ListNode(x)
  20.             cur = head
  21.             while n > 0:
  22.                 x, n = n % 10, n // 10
  23.                 newNode = ListNode(x)
  24.                 cur.next = newNode
  25.                 cur = newNode
  26.             return head
  27.            
  28.         n1, n2 = list2num(l1), list2num(l2)
  29.         s = n1 + n2
  30.         return num2list(s)
Advertisement
Add Comment
Please, Sign In to add comment