-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdd Two Numbers
More file actions
32 lines (30 loc) · 823 Bytes
/
Copy pathAdd Two Numbers
File metadata and controls
32 lines (30 loc) · 823 Bytes
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
# 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:
s1=''
while(l1 is not None):
s1+=str(l1.val)
l1=l1.next
s2=''
while(l2 is not None):
s2+=str(l2.val)
l2=l2.next
ans=int(s1[::-1])+int(s2[::-1])
ans=str(ans)
# createlist(ans)
head=None
sec=None
for x in ans:
if(head==None):
head=ListNode(int(x))
# head.next=None
else:
sec=ListNode(int(x))
sec.next=head
head=sec
sec=None
return head