MergeSort Linked List

For a given Singly Linked List of integers, sort the list using the 'Merge Sort' algorithm.

Input format :
The first and the only line of input contains the elements of the linked list separated by a single space. 
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element
Output format :
The only line of output contains the sorted elements of the Singly Linked List in a row, separated by a single space.
Note:
You are not required to print the elements, just sort the elements and return the head to the updated Singly Linked List.
Constraints :
1 <= N <= 10^5
1 <= DATA <= 10^9

Where 'DATA' denotes the value of node of Linked List.

Time Limit: 1sec
CodingNinjas
author
2y
Merge Sort Linked List
  1. The idea is to take a recursive approach by calling the sort function on the left half and right half of the list.
  2. In order to do so, get the middle node and split the list into t...read more
Help your peers!
Add answer anonymously...
Samsung Research Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter