So I have made a function to traverse through a linked list, recursively and at every traversal I concatenate the value to a string (empty string during initialization), the function seems to be working properly. I added a print statement in the base case to see what the value of the variable before the return statement, here is the code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1, l2):
s1 = ""
s2 = ""
def num(l, s):
if(l==None):
print(s)
return s
s = s+(str(l.val))
num(l.next, s)
print(num(l1,s1))
This is the Output