Add Linked Lists
Given two numbers represented by linked lists. Your task is find the sum list and return the head of the sum list.
The sum list is a linked list representation of addition of two numbers.
Input Format :
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains the elements of the first singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line of each test case contains the elements of the second singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
Output Format :
For each test case, print the sum linked list. The elements of the sum list should be single-space separated, terminated by -1.
The output of each test case is printed in a separate line.
Note :
You do not need to print anything, just return the head of the sum linked list.
Constraints :
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= data <= 9 and data != -1
Time Limit : 1sec
CodingNinjas
author
2y
Recursive approach
One way is to recursively add the two linked lists. Keep the nodes in the recursion stack and add the last nodes first and then second last and so on. Initially, find the size of bot...read more
CodingNinjas
author
2y
Iterative approach
The idea is to use the simple method of adding two numbers. We add two numbers starting from their least significant digit and moving towards the most significant digit. So we need t...read more
Help your peers!
Add answer anonymously...
Top Josh Technology Group Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
>
Josh Technology Group Software Developer Intern 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