Add Two Numbers II
Lists l1 and l2 each hold the digits of a non-negative number with the most significant digit first (the usual reading order). Add the two numbers and return the sum as a list in the same format, with no leading zeros unless the sum is 0.
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 [7,2,4,3]
l2 [5,6,4]
returns [7,8,0,7]
7243 + 564 = 7807.
Example 2
l1 [9,9]
l2 [1]
returns [1,0,0]
99 + 1 = 100.
Constraints
1 <= nodes in each list <= 200 <= Node.val <= 9- Neither number has leading zeros, 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
- 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
- Odd Even Linked ListMedium