Add Two Numbers Using Linked Lists
Mediumjavapythonccppjavascript
Two non-negative integers are represented by linked lists, one digit per node, with the most significant digit first. Add the two numbers and print the digits of the sum, most significant digit first.
Input Format
The first line contains an integer N. The second line contains N space-separated digits (0-9), the digits of number A, most significant first. The third line contains an integer M. The fourth line contains M space-separated digits (0-9), the digits of number B, most significant first.
Output Format
Print the digits of A + B, most significant first, separated by single spaces, with no leading zeros (print 0 if the sum is zero).
Example 1
Input
3 2 4 3 3 5 6 4
Output
8 0 7
Explanation: The numbers are 243 and 564; their sum is 807.
- 1 <= N, M <= 5000
- Each digit is between 0 and 9
- The leading digit is non-zero unless the number is exactly 0