`{"id":"895b32fc-b24f-4a48-85e7-00eaf13034bc","name":"Mergesort Linkedlist","description":"Given the head of a linked list, return the list after sorting it in increasing order.\r\nTime Complexity : O(nlogn)\r\nSpace Complexity : constant space \r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *mergeSort(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n\r\n ListNode *head = mergeSort(h1);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode mergeSort(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n ListNode head = mergeSort(h1);\r\n printList(head);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 5 6 7 ","questionVideo":"https://www.youtube.com/embed/HUFibUCDt0U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"a71d841d-1fca-4f90-b6c3-b55f9d182d2b","name":"Mergesort Linkedlist","slug":"mergesort-linkedlist","type":1}],"next":{"id":"b729a792-369e-43cb-a8db-905e7df72bcf","name":"Merge Sort Linked list MCQ","type":0,"slug":"merge-sort-linked-list-mcq"},"prev":{"id":"1a8d27ae-31f9-4046-8967-c504f9515a7f","name":"Merge Two Sorted Linked Lists","type":3,"slug":"merge-two-sorted-linked-lists"}}}`

Given the head of a linked list, return the list after sorting it in increasing order. Time Complexity : O(nlogn) Space Complexity : constant space

`{"id":"895b32fc-b24f-4a48-85e7-00eaf13034bc","name":"Mergesort Linkedlist","description":"Given the head of a linked list, return the list after sorting it in increasing order.\r\nTime Complexity : O(nlogn)\r\nSpace Complexity : constant space \r\n","inputFormat":"1->7->2->6->3->5->4->null\r\n","outputFormat":"1->2->3->4->5->6->7->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *mergeSort(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *h1 = createList(n);\r\n\r\n ListNode *head = mergeSort(h1);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode mergeSort(ListNode head) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n\r\n ListNode head = mergeSort(h1);\r\n printList(head);\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n0\r\n6\r\n7\r\n5\r\n","sampleOutput":"0 5 6 7 ","questionVideo":"https://www.youtube.com/embed/HUFibUCDt0U","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"a71d841d-1fca-4f90-b6c3-b55f9d182d2b","name":"Mergesort Linkedlist","slug":"mergesort-linkedlist","type":1}],"next":{"id":"b729a792-369e-43cb-a8db-905e7df72bcf","name":"Merge Sort Linked list MCQ","type":0,"slug":"merge-sort-linked-list-mcq"},"prev":{"id":"1a8d27ae-31f9-4046-8967-c504f9515a7f","name":"Merge Two Sorted Linked Lists","type":3,"slug":"merge-two-sorted-linked-lists"}}}`

Editor

easy

Given the head of a linked list, return the list after sorting it in increasing order. Time Complexity : O(nlogn) Space Complexity : constant space

0 <= N <= 10^6

## Format

### Input

1->7->2->6->3->5->4->null

### Output

1->2->3->4->5->6->7->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 0 6 7 5 ```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0 5 6 7 `

Question Video

Discussions

Show Discussion

Related Resources