Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-065

Add Two Numbers as Lists

Problem

Add two non-negative integers whose digits are stored as linked lists in least-significant-first order.

a and b are lists of decimal digits with the ones digit first. Walk both lists with a carry and append sum % 10 at each step, continuing while any digits or carry remain.

Return the sum as a digit list in the same least-significant-first order.

Hint

Process one digit per list per step and keep the carry until both lists and the carry are exhausted.

Test cases

case 1
in: [[2,4,3],[5,6,4]]
expected: [7,0,8]
case 2
in: [[0],[0]]
expected: [0]
case 3
in: [[9,9],[1]]
expected: [0,0,1]
case 4
in: [[9],[9]]
expected: [8,1]
case 5
in: [[1,8],[0]]
expected: [1,8]
Starter codePython
def add_two_numbers(a, b):
    # Your code here
    pass

5 test cases · run your code against them in the editor