Merge Sort Linked List Problem Statement

You are given a singly linked list of integers. Your task is to sort the linked list using the merge sort algorithm.

Explanation:

Merge Sort is a divide and conquer algorithm. It divides the input into two halves, calls itself for the two halves, and then merges the two sorted halves. The merge() function is critical for merging two halves. It works by assuming that the subarrays arr[L..M] and arr[M + 1...R] are sorted and merges these two sorted subarrays into one.

Input:

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 singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.

Output:

For each test case, print the sorted linked list. The elements of the sorted list should be single-space separated, terminated by -1.

The output of each test case is printed in a separate line.

Example:

To illustrate, given an input test case linked list of 3 1 2 -1, the output will be 1 2 3 -1.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4
  • -10^9 <= data <= 10^9 and data != -1

Note:

You are not required to print the output; it has already been taken care of. Just implement the function.
AnswerBot
4mo

Implement merge sort algorithm for singly linked list of integers.

  • Divide the linked list into two halves using slow and fast pointer technique.

  • Recursively sort the two halves.

  • Merge the sorted halves u...read more

Help your peers!
Select
Add answer anonymously...

Sourcefuse Technologies Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Delete a Node from a Linked List You are provided with a linked list of integers...read more
A Software Developer Intern was asked Q. Merge Sort Linked List Problem Statement You are given a singly linked list of i...read more
A Software Developer Intern was asked Q. Palindrome Substrings Given a string STR, your objective is to determine the tot...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Delete a Node from a Linked List You are provided with a linked list of integers...read more
A Software Developer Intern was asked Q2. Merge Sort Linked List Problem Statement You are given a singly linked list of i...read more
A Software Developer Intern was asked Q3. Palindrome Substrings Given a string STR, your objective is to determine the tot...read more
Sourcefuse Technologies 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