Merge Two Sorted Linked Lists Problem Statement

You are provided with two sorted linked lists. Your task is to merge them into a single sorted linked list and return the head of the combined linked list.

Input:

The first line indicates the integer 'T', which is the number of test cases. Each test case consists of: 
The first line contains the elements of the first linked list, separated by spaces, ending with -1.
The second line contains the elements of the second linked list, separated by spaces, ending with -1.

Output:

For each test case, output the merged linked list, with elements separated by spaces, ending with -1. Each test case's output should be on a new line.

Example:

Input:
1 4 5 -1
2 3 5 -1
Output:
1 2 3 4 5 5 -1

Constraints:

  • 1 <= T <= 10
  • 1 <= L <= 10^5
  • 1 ≤ data ≤ 10^6 and data != -1

Note:

The provided linked lists may be empty, represented by ending immediately with -1.

Follow-up:

Attempt to solve this problem using a method with linear time complexity and without additional space usage beyond constant space complexity.
AnswerBot
4mo

Merge two sorted linked lists into a single sorted linked list without using additional space.

  • Create a dummy node to start the merged list

  • Compare the values of the two linked lists and add the smaller...read more

Anonymous
10mo
Algorithm: Step 1:Initialise an empty array and iterate through each node of the first and second linked list adding them to the array. Step 2: Sort the array in ascending order, using a sorting algor...read more
Help your peers!
Select
Add answer anonymously...

CARS24 Software Developer interview questions & answers

A Software Developer was asked 11mo agoQ. Implement a Spring Boot service that intercepts HTTP requests.
A Software Developer was asked 11mo agoQ. Implement an immutable class.
A Software Developer was asked Q. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more

Popular interview questions of Software Developer

A Software Developer was asked 11mo agoQ1. Implement a Spring Boot service that intercepts HTTP requests.
A Software Developer was asked 11mo agoQ2. Implement an immutable class.
A Software Developer was asked Q3. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
CARS24 Software Developer 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