
Asked in Deutsche Bank
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...
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

