Create add_two_numbers.py

This commit is contained in:
marina 2023-08-02 20:37:09 -07:00 committed by GitHub
parent e5c9084b64
commit b068e60212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,37 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Node:
def __init__(self, val):
self.val = val
self.next = None
def add_two_numbers(l1, l2):
n1, n2, i = '', '', 1
while l1:
n1 += str(l1.val)
l1 = l1.next
while l2:
n2 += str(l2.val)
l2 = l2.next
n1 = int(n1[::-1])
n2 = int(n2[::-1])
n = str(n1 + n2)[::-1]
current = ListNode(n[0])
head = current
while i < len(n):
current.next = ListNode(n[i])
current = current.next
i += 1
return head