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
- ll1 = list(listiter(l1))
- ll2 = list(listiter(l2))
- if len(ll2) < len(ll1):
- ll1, ll2 = ll2, ll1
- for _ in range(len(ll2) - len(ll1)):
- ll1.append(0)
- head = ListNode(-1)
- cur = head
- carry = 0
- for x, y in zip(ll1, ll2):
- 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