Add Two Numbers Represented as Linked Lists
Given two linked lists representing two non-negative integers, where the digits are stored in reverse order (i.e., starting from the least significant digit to the most significant digit), each node contains a single digit. Your task is to add these two numbers and return the head of the linked list representing the sum of the numbers.
Example:
Input:
TestCase 1:
List1: 2 -> 4 -> 3 -> -1
List2: 5 -> 6 -> 4 -> -1
Output:
7 -> 0 -> 8 -> -1
Explanation:
The numbers are 342 and 465. The sum is 807, and the linked list representation of the sum is 7 -> 0 -> 8 -> -1
Input:
The first line contains an integer T, the number of test cases.
Each test case consists of:
- Two lines containing elements of the first and second linked list respectively, terminated by -1.
Output:
For each test case, output the summed linked list, with elements space-separated and terminated by -1.
Constraints:
1 <= T <= 10
1 <= M, N <= 5 * 104
0 <= data[i] <= 9
anddata[i] != -1
Where 'M' and 'N' are the number of nodes in the two linked lists, 'data[i]' is the data of the 'i-th' node.
Note: The numbers do not have leading zeros.
Be the first one to answer
Add answer anonymously...
Popular interview questions of Associate Professional
>
Centre for Development of Advanced Computing Associate Professional Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app