Skip to content

Commit 8613f07

Browse files
authored
Create Add_Two_Numbers.py
1 parent 5469431 commit 8613f07

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Linked_Lists/Add_Two_Numbers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Solution - 1: O(N + M) Time and O(1) Space
2+
class Solution:
3+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
4+
carry = 0
5+
dummy = ListNode(0)
6+
t_head = dummy
7+
while l1 or l2 or carry:
8+
if l1:
9+
carry += l1.val
10+
l1 = l1.next
11+
if l2:
12+
carry += l2.val
13+
l2 = l2.next
14+
t_head.next = ListNode(carry % 10)
15+
carry = carry//10
16+
t_head = t_head.next
17+
18+
return dummy.next
19+

0 commit comments

Comments
 (0)