Is its run time ==O(n)?


  • 0
    D
    # Definition for singly-linked list.
    

    class ListNode(object):

    def init(self, x):

    self.val = x

    self.next = None

    class Solution(object):

    def revString(self,s):
        rStr=''
        for i in range(len(s)-1,-1,-1):
            rStr+=s[i]
            
        return rStr
        
    def getNum(self,node):
        #print node.val,
        #print node.next.val
        
        
        num1='';
        while 1:
            if node==None:
                break
            curr_value=node.val
            num1+=str(curr_value)
            nxt_node=node.next
            node=nxt_node
            
        #print num1
        num=self.revString(num1)
        return num    
           
            
            
    def getNodeArray(self,num):
        numS=str(num)
        #print numS
        numR=[]
        if len(numS)==1:
            
            numR.append(int(numS[0]))
        else:
            
            for i in range(len(numS)-1,-1,-1):
                #print(numS[i])
                numR.append(int(numS[i]))
        #print numR    
        return numR
            
        
        
            
        
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        num1=self.getNum(l1)
        num2=self.getNum(l2)
        
        #print num1
        #print num2
        num=int(num1)+int(num2)
        numNode=self.getNodeArray(num)
        return numNode
        
        
    
        #print(num1)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.