`{"id":"8bb24dce-e105-4295-a6d8-326c90c3f277","name":"Merge K Sorted Linkedlist","description":"You are given an array of k linked-lists, each linked-list is sorted in increasing order.\r\nMerge all the linkedlists into one sorted linkedlist and return it.\r\n","inputFormat":"3 sorted linkedlist :\r\n{\r\n 0->0->0->null,\r\n 0->0->1->1->1->2->2->4->null,\r\n 0->0->0->0->5->5->6->null\r\n}","outputFormat":"after merging them : \r\n0->0->0->0->0->0->0->0->0->1->1->1->2->2->4->5->5->6->null","constraints":"0 &lt;= k &lt;= 10^5\r\n0 &lt;= size of linkedlist &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n\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 *mergeKLists(vector<ListNode *> &lists)\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 vector<ListNode *> list(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n int m;\r\n cin >> m;\r\n list[i] = createList(m);\r\n }\r\n\r\n ListNode *head = mergeKLists(list);\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 mergeKLists(ListNode[] lists) {\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[] list = new ListNode[n];\r\n for (int i = 0; i < n; i++) {\r\n int m = scn.nextInt();\r\n list[i] = createList(m);\r\n }\r\n\r\n ListNode head = mergeKLists(list);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n3\r\n0 0 0 \r\n8\r\n0 0 1 1 1 2 2 4 \r\n7\r\n0 0 0 0 5 5 6 \r\n","sampleOutput":"0 0 0 0 0 0 0 0 0 1 1 1 2 2 4 5 5 6 ","questionVideo":"","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":"9d5bcd36-2647-4a08-a155-52d51542bc7b","name":"Merge K Sorted Linkedlist","slug":"merge-k-sorted-linkedlist","type":1}],"next":{"id":"f56d5968-8574-4aa1-989c-f8b7f200b377","name":"Merge K Sorted Linkedlist MCQ","type":0,"slug":"merge-k-sorted-linkedlist-mcq"},"prev":{"id":"606e4260-0539-4fec-a42a-b7a653a9dfa2","name":"Merge Sort A Linked List","type":3,"slug":"merge-sort-a-linked-list"}}}`

You are given an array of k linked-lists, each linked-list is sorted in increasing order. Merge all the linkedlists into one sorted linkedlist and return it.

`{"id":"8bb24dce-e105-4295-a6d8-326c90c3f277","name":"Merge K Sorted Linkedlist","description":"You are given an array of k linked-lists, each linked-list is sorted in increasing order.\r\nMerge all the linkedlists into one sorted linkedlist and return it.\r\n","inputFormat":"3 sorted linkedlist :\r\n{\r\n 0->0->0->null,\r\n 0->0->1->1->1->2->2->4->null,\r\n 0->0->0->0->5->5->6->null\r\n}","outputFormat":"after merging them : \r\n0->0->0->0->0->0->0->0->0->1->1->1->2->2->4->5->5->6->null","constraints":"0 &lt;= k &lt;= 10^5\r\n0 &lt;= size of linkedlist &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n\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 *mergeKLists(vector<ListNode *> &lists)\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 vector<ListNode *> list(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n int m;\r\n cin >> m;\r\n list[i] = createList(m);\r\n }\r\n\r\n ListNode *head = mergeKLists(list);\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 mergeKLists(ListNode[] lists) {\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[] list = new ListNode[n];\r\n for (int i = 0; i < n; i++) {\r\n int m = scn.nextInt();\r\n list[i] = createList(m);\r\n }\r\n\r\n ListNode head = mergeKLists(list);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n3\r\n0 0 0 \r\n8\r\n0 0 1 1 1 2 2 4 \r\n7\r\n0 0 0 0 5 5 6 \r\n","sampleOutput":"0 0 0 0 0 0 0 0 0 1 1 1 2 2 4 5 5 6 ","questionVideo":"","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":"9d5bcd36-2647-4a08-a155-52d51542bc7b","name":"Merge K Sorted Linkedlist","slug":"merge-k-sorted-linkedlist","type":1}],"next":{"id":"f56d5968-8574-4aa1-989c-f8b7f200b377","name":"Merge K Sorted Linkedlist MCQ","type":0,"slug":"merge-k-sorted-linkedlist-mcq"},"prev":{"id":"606e4260-0539-4fec-a42a-b7a653a9dfa2","name":"Merge Sort A Linked List","type":3,"slug":"merge-sort-a-linked-list"}}}`

Editor

easy

You are given an array of k linked-lists, each linked-list is sorted in increasing order. Merge all the linkedlists into one sorted linkedlist and return it.

## Constraints

0 <= k <= 10^5 0 <= size of linkedlist <= 1000

## Format

### Input

3 sorted linkedlist : { 0->0->0->null, 0->0->1->1->1->2->2->4->null, 0->0->0->0->5->5->6->null }

### Output

after merging them : 0->0->0->0->0->0->0->0->0->1->1->1->2->2->4->5->5->6->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;}3 3 0 0 0 8 0 0 1 1 1 2 2 4 7 0 0 0 0 5 5 6 ```

### 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 0 0 0 0 0 0 0 0 1 1 1 2 2 4 5 5 6 `

Discussions

Show Discussion

Related Resources