Add Two Numbers Represented by Linked Lists

Your task is to find the sum list of two numbers represented by linked lists and return the head of the sum list.

Explanation:

The sum list should be a linked list representation of the addition of the two numbers.

Input:

The first line of input contains a single integer T, indicating the number of test cases to be processed.

For each test case, there are two lines of input:
- The first line contains the elements of the first linked list, separated by spaces and ending with -1. The -1 is not part of the list elements.
- The second line contains the elements of the second linked list, also separated by spaces and ending with -1.

Output:

For each test case, print the sum linked list with elements separated by spaces and ending with -1.
Each test case should have its output on a new line.

Example:

Input:
2
7 5 9 4 6 -1
8 4 -1
5 6 3 -1
8 4 2 -1

Output:
5 0 0 5 0 -1
1 4 0 6 -1

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 5 * 104
  • 0 ≤ data ≤ 9 and data ≠ -1

Note:

You are not required to print anything; return the head of the sum linked list.
Be the first one to answer
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Josh Technology Group

Q. Given a linked list, how do you ensure that it is in decreasing order?
Q. Given a random node in a BST, how do you check if a given element is part of the...read more
Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits