forked from LisCoding/Interview-Algo-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTwoNumbers.py
35 lines (29 loc) · 857 Bytes
/
addTwoNumbers.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
output = ListNode(None)
temp = output
addOne = False
while (l1 or l2):
a = 0
if l1:
a += l1.val
l1 = l1.next
if l2:
a += l2.val
l2 = l2.next
if addOne:
a += 1
addOne = False
if a > 9:
a = a % 10
addOne = True
temp.next = ListNode(a)
temp = temp.next
if addOne:
temp.next = ListNode(1)
return output.next