Skip to contentKEEP THE CRAFTTHE SANCTUARY

Add Two Numbers

Add two numbers stored as reversed digit lists, carrying just like column addition.

The problem

Implement Solution.addTwoNumbers(l1, l2). Each linked list holds a non-negative integer with its digits in reverse order, so the head is the ones digit: 2 -> 4 -> 3 is 342. Return a new linked list, also in reverse order, that holds the sum of the two numbers. For example, 2 -> 4 -> 3 plus 5 -> 6 -> 4 is 342 + 465 = 807, returned as 7 -> 0 -> 8. Each list has 1 to 100 nodes, every value is a digit from 0 to 9, and no number has leading zeros except the number 0 itself.

Starter code

class Solution:
    def addTwoNumbers(self, l1, l2):
        pass

Public examples

  1. l1 = [2, 4, 3], l2 = [5, 6, 4]gives[7, 0, 8]
  2. l1 = [4, 2], l2 = [1, 3]gives[5, 5]
  3. l1 = [9], l2 = [8]gives[7, 1]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.