Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

sum two linked lists if numbers are reversed in linked list

    def sum_two_lists(self, llist):
        cur = self.head
        cur1 = llist.head
        n1 = 0
        n2 = 0
        remainder = 1
        while cur:
            n1 += (cur.data) * remainder
            remainder *= 10
            cur = cur.next
        remainder = 1
        while cur1:
            n2 += (cur1.data) * remainder
            remainder *= 10
            cur1 = cur1.next
        total = str(n1 + n2)[::-1]
        answer = LinkedList()
        for i in total:
            answer.append(i)
        return answer
 
PREVIOUS NEXT
Tagged: #sum #linked #lists #numbers #reversed #linked #list
ADD COMMENT
Topic
Name
3+7 =