Add two non-negative integers represented as reversed linked lists and return the sum in the same format.
Problem
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit.
Add the two numbers and return the sum as a linked list in the same reversed-digit format.
You may assume the input numbers do not contain any leading zeroes except for the number 0 itself.
Notes
- Each linked list stores digits from least significant to most significant.
- The result should also be stored in reverse order.
- You must handle carry across nodes when the digit sum is 10 or greater.
Input Format
- Two singly linked lists
l1andl2. - Each node stores one digit in reverse order.
- The lists represent two non-negative integers.
Output Format
- Return a singly linked list representing the sum.
- The digits must also be in reverse order.
Constraints
- The input lists are non-empty.
- Each node value is an integer digit from
0to9. - The represented numbers are non-negative.
- The output should not contain unnecessary leading zeroes.
Example 1
Input
l1 = [2,4,3], l2 = [5,6,4]
Output
[7,0,8]
Explanation
342 + 465 = 807, so the reversed linked-list form is 7 -> 0 -> 8.
Example 2
Input
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output
[8,9,9,9,0,0,0,1]
Explanation
9999999 + 9999 = 10009998, which becomes 8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.