-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.py
More file actions
66 lines (56 loc) · 1.58 KB
/
Copy path2.py
File metadata and controls
66 lines (56 loc) · 1.58 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
current=dummy=ListNode()
remainder=0
while l1 and l2:
sum=l1.val+l2.val+remainder
if sum>9:
remainder=1
sum-=10
else:
remainder=0
current.next=ListNode(sum)
l1=l1.next
l2=l2.next
current=current.next
#Check if one of the lists has remaining nodes, if yes add them
if l1 or l2:
while l1 or l2:
if l1:
sum=l1.val+remainder
l1=l1.next
else:
sum=l2.val+remainder
l2=l2.next
if sum>9:
remainder=1
sum-=10
else:
remainder=0
current.next=ListNode(sum)
current=current.next
if remainder==1:
current.next=ListNode(1)
return dummy.next
"""
l1=ListNode(9)
l1.next=ListNode(9)
l1.next.next=ListNode(9)
l1.next.next.next=ListNode(9)
l1.next.next.next.next=ListNode(9)
l1.next.next.next.next.next=ListNode(9)
l1.next.next.next.next.next.next=ListNode(9)
l2=ListNode(9)
l2.next=ListNode(9)
l2.next.next=ListNode(9)
l2.next.next.next=ListNode(9)
solution_instance=Solution()
head=solution_instance.addTwoNumbers(l1,l2)
while head:
print(head.val)
head=head.next
"""