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 to sort a singly linked list of integers.

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

  • Recursively sort the two halves.

  • Merge the sorted halves usin...read more

Help your peers!
Select
Add answer anonymously...

Nagarro Software Developer interview questions & answers

A Software Developer was asked 3mo agoQ. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ. What is abstraction in Java?

Popular interview questions of Software Developer

A Software Developer was asked 3mo agoQ1. Design and implement a data structure for Least Recently Used (LRU) cache. It sh...read more
A Software Developer was asked 3mo agoQ2. Given the root of a binary search tree, and an integer k, return the kth smalles...read more
A Software Developer was asked 5mo agoQ3. What is abstraction in Java?
Nagarro 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