Merge Two Sorted Lists (In-Place) Problem Statement

You are given two sorted linked lists. Your task is to merge them to form a combined sorted linked list and return the head of this final linked list.

Example:

Input:
First list: 1 -> 4 -> 5 -> NULL
Second list: 2 -> 3 -> 5 -> NULL
Output:
1 -> 2 -> 3 -> 4 -> 5 -> 5 -> NULL
Explanation:

The nodes from both input lists are merged into a single sorted linked list.

Constraints:

  • 1 <= T <= 10
  • 1 <= L <= 105
  • 1 ≤ data ≤ 106 and data ≠ -1

Input:

The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Each test case consists of:
The first line contains the elements of the first linked list, separated by spaces, and terminated by -1.
The second line contains the elements of the second linked list, separated by spaces, and terminated by -1.

Output:

For each test case, print the merged final linked list. The elements of the list must be separated by a single space and terminated by -1.

Note:

You do not need to print the result yourself; implement the function returning the head of the merged list.

Follow-up:

Try to solve this problem in linear time complexity and constant space complexity.

AnswerBot
4mo

Merge two sorted linked lists in-place to form a combined sorted linked list.

  • Create a dummy node to start the merged list

  • Compare nodes from both lists and link them accordingly

  • Update the pointer to th...read more

Help your peers!
Select
Add answer anonymously...

Publicis Sapient Software Developer interview questions & answers

A Software Developer was asked 9mo agoQ. What are the SOLID principles?
A Software Developer was asked Q. What is the difference between a List and a Tuple?
A Software Developer was asked Q. What are extension methods?

Popular interview questions of Software Developer

A Software Developer was asked 10mo agoQ1. What are the SOLID principles?
A Software Developer was asked Q2. What are extension methods?
A Software Developer was asked Q3. Given the head of a singly linked list, reverse the list, and return the reverse...read more

Top HR questions asked in Publicis Sapient Software Developer

A Software Developer was asked 9mo agoQ1. When can you join the department?
A Software Developer was asked 9mo agoQ2. How do you handle pressure?
A Software Developer was asked Q3. What are your weaknesses?
Publicis Sapient 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