Add Two Numbers

Medium

This is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.

Question

You're given two linked lists, l1 and l2, where each node holds a single digit of a non-negative integer. The digits are stored in reverse order. That means the head of each list holds the ones digit.

Add the two numbers together and return the sum as a linked list in the same reverse-digit format.

Note: Neither input list is empty. The two lists can have different lengths. Neither number has a leading zero unless the number itself is 0.

Input: l1 = [2 -> 4 -> 3], l2 = [5 -> 6 -> 4]

Output: [7 -> 0 -> 8]

342 + 465 = 807, stored as [7 -> 0 -> 8].

Input: l1 = [5], l2 = [5]

Output: [0 -> 1]

5 + 5 = 10, stored as [0 -> 1].

Input: l1 = [9 -> 9 -> 9], l2 = [1]

Output: [0 -> 0 -> 0 -> 1]

999 + 1 = 1000, stored as [0 -> 0 -> 0 -> 1].

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What does adding l1 = [8 -> 9] and l2 = [1] produce? Remember, digits are stored ones-first.
[9 -> 9]
[9 -> 0 -> 1]
[8, 10]
[0 -> 0 -> 1]

Remember to understand the problem and plan your approach before you start coding.