-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-linked-lists.py
More file actions
25 lines (23 loc) · 845 Bytes
/
Copy pathadd-linked-lists.py
File metadata and controls
25 lines (23 loc) · 845 Bytes
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
# https://binarysearch.com/problems/Add-Linked-Lists
# class LLNode:
# def __init__(self, val, next=None):
# self.val = val
# self.next = next
class Solution:
def helper(self, l0, l1, carry):
if not l0 and not l1:
if carry == 1:
return LLNode(1)
else:
return None
elif l0 and l1:
sum_ = l0.val + l1.val + carry
return LLNode(sum_ % 10, self.helper(l0.next, l1.next, sum_ // 10))
elif l0 and not l1:
sum_ = l0.val + carry
return LLNode(sum_ % 10, self.helper(l0.next, None, sum_ // 10))
elif not l0 and l1:
sum_ = l1.val + carry
return LLNode(sum_ % 10, self.helper(None, l1.next, sum_ // 10))
def solve(self, l0, l1):
return self.helper(l0, l1, 0)