Add Two Numbers
Lists l1 and l2 each hold the digits of a non-negative number in reverse order: the head is the ones digit, the next node is the tens digit, and so on. Add the two numbers and return the sum in the same reversed-digit list format.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
l1 [2,4,3]
l2 [5,6,4]
returns [7,0,8]
342 + 465 = 807, stored as 7, 0, 8.
Example 2
l1 [9,9]
l2 [1]
returns [0,0,1]
99 + 1 = 100, stored as 0, 0, 1.
Constraints
1 <= nodes in each list <= 200 <= Node.val <= 9- Neither number has extra zeros at its most significant end, except the number 0 itself
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Linked lists problems
- Add Two Numbers IIMedium
- Cancel Opposite NeighborsMedium
- Delete the Middle NodeMedium
- Evens Before OddsMedium
- Last One StandingMedium
- Length of the CycleMedium
- Maximum Twin SumMedium
- Move a Value to the EndMedium