Add Two Numbers
Medium · Linked List
Each of two linked lists represents a non-negative integer with its digits stored in reverse order, one digit per node. Add the two numbers and return the sum as a new linked list, also with digits in reverse order. Assume neither input list has a leading zero unless the number itself is zero.
Examples
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Why: 342 plus 465 equals 807, and 807 written with least significant digit first is [7,0,8].
Input: l1 = [0], l2 = [0]
Output: [0]
Why: 0 plus 0 is 0.
Input: l1 = [9,9,9,9], l2 = [9,9]
Output: [8,9,0,0,1]
Why: 9999 plus 99 equals 10098, and reversed digit order gives [8,9,0,0,1].
Constraints
1 <= number of nodes in each list <= 100, 0 <= node value <= 9, the lists do not contain leading zeros unless the number itself is 0
Practise it by voice
Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.
This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Add Two Numbers. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.