Skip to content

Instantly share code, notes, and snippets.

@DataSolveProblems
Last active September 17, 2019 22:32
Show Gist options
  • Save DataSolveProblems/09e1d725740467884d8d3db388d5790c to your computer and use it in GitHub Desktop.
Save DataSolveProblems/09e1d725740467884d8d3db388d5790c to your computer and use it in GitHub Desktop.
class ListNode():
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
prev = result = ListNode(None)
n = 0
while l1 or l2 or n:
if l1:
n += l1.val
l1 = l1.next
if l2:
n += l2.val
l2 = l2.next
prev.next = ListNode(n % 10)
prev = prev.next
n //= 10
return result.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment