-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers.py
37 lines (35 loc) · 1.09 KB
/
add-two-numbers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
lFinal = ListNode()
temp = lFinal
while l1 != None or l2 != None:
if l1 != None:
self.chainAdd(temp, l1.val)
l1 = l1.next
if l2 != None:
self.chainAdd(temp, l2.val)
l2 = l2.next
if temp.next == None and (l1 != None or l2 != None):
temp.next = ListNode()
temp = temp.next
return lFinal
def chainAdd(self, lis, num):
if lis.val + num > 9:
if lis.next != None:
lis.val = lis.val + num - 10
self.chainAdd(lis.next, 1)
else:
lis.val = lis.val + num - 10
lis.next = ListNode(val = 1)
else:
lis.val = lis.val + num