Skip to content

Instantly share code, notes, and snippets.

@elvisfromsouth
Created September 17, 2021 09:09
Show Gist options
  • Save elvisfromsouth/7bfc753dc5022d4c71002befac992f4e to your computer and use it in GitHub Desktop.
Save elvisfromsouth/7bfc753dc5022d4c71002befac992f4e to your computer and use it in GitHub Desktop.
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0)
currentNode = dummy
extra = 0
while l1 or l2:
currentSum = extra
if l1:
currentSum += l1.val
l1 = l1.next
if l2:
currentSum += l2.val
l2 = l2.next
currentNode.next = ListNode(currentSum % 10)
currentNode = currentNode.next
extra = currentSum // 10
if extra > 0:
currentNode.next = ListNode(extra)
return dummy.next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment