-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers.py
52 lines (40 loc) · 952 Bytes
/
add-two-numbers.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# https://leetcode.com/problems/add-two-numbers
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def addTwoNumbers(l1 , l2):
x=''
y=''
while l1!=None:
x+=str(l1.val)
l1=l1.next
while l2!=None:
y+=str(l2.val)
l2=l2.next
# list is reversed
x=x[::-1]
y=y[::-1]
# add and reverse
sum = str(int(x) + int(y))[::-1]
sum_list=None
pointer=sum_list
for digit in sum:
if sum_list==None:
sum_list=ListNode(digit)
pointer=sum_list
else:
pointer.next=ListNode(digit)
pointer=pointer.next
return(sum_list)
l1=ListNode(2)
l1.next=ListNode(4)
l1.next.next=ListNode(3)
l2=ListNode(5)
l2.next=ListNode(6)
l2.next.next=ListNode(4)
l = addTwoNumbers(l1,l2)
while l!=None:
print(str(l.val),end='')
l=l.next