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):
- from itertools import izip
- def listiter(l):
- cur = l
- while cur:
- yield (False, cur.val)
- cur = cur.next
- while 1:
- yield (True, 0)
- head = ListNode(-1)
- cur = head
- carry = 0
- for (e1, x), (e2, y) in izip(listiter(l1), listiter(l2)):
- if e1 and e2:
- break
- s = x + y + carry
- s, carry = s % 10, s // 10
- node = ListNode(s)
- cur.next = node
- cur = node
- if carry > 0:
- node = ListNode(carry)
- cur.next = node
- return head.next
Advertisement
Add Comment
Please, Sign In to add comment