-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-Add_Two_Numbers.py
More file actions
60 lines (56 loc) · 1.46 KB
/
2-Add_Two_Numbers.py
File metadata and controls
60 lines (56 loc) · 1.46 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
# 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:
l3=ListNode(0)
n3=l3
carry=0
while l2!=None and l1!=None:
l3.next=ListNode(carry)
carry=0
l3=l3.next
l3.val=l3.val+l1.val+l2.val
if l3.val>=10:
l3.val=l3.val-10
carry=1
l1=l1.next
l2=l2.next
if l1==None:
while l2!=None:
l3.next=ListNode(carry)
carry=0
l3=l3.next
l3.val=l3.val+l2.val
if l3.val>=10:
l3.val=l3.val-10
carry=1
l2=l2.next
else:
while l1!=None:
l3.next=ListNode(carry)
carry=0
l3=l3.next
l3.val=l3.val+l1.val
if l3.val>=10:
l3.val=l3.val-10
carry=1
l1=l1.next
if carry==1:
l3.next=ListNode(carry)
carry=0
return n3.next
l1=ListNode(5)
#l1.next=ListNode(4)
#l1.next.next=ListNode(3)
l2=ListNode(5)
#l2.next=ListNode(6)
#l2.next.next=ListNode(4)
mysolution=Solution()
l3=mysolution.addTwoNumbers(l1,l2)
n3=l3
while l3!=None:
print(l3.val)
l3=l3.next