Merge k Sorted Lists Hard / #23 / merge k lists
You get k linked lists, each sorted in ascending order; some may be empty. Join all their nodes into one sorted linked list and return its head. Our example: 2 → 6, an empty list, 1 → 4 → 7, 3 → 5 — the answer is 1 → 2 → 3 → 4 → 5 → 6 → 7.
input lists: k linked lists, each sorted in non-decreasing order; any of them may be empty, and so may lists. returns The head of one linked list with all their values, sorted in non-decreasing order. constraints 0 ≤ k ≤ 50 0 ≤ nodes in each list ≤ 100, ≤ 1000 in all −10⁴ ≤ Node.val ≤ 10⁴ each list is sorted in non-decreasing order our 13 tests keep to them examples Open a test in the tests panel: it shows the input and the expected answer. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
function mergeKLists(lists) {
}
test submit · off
13 tests · limit 3 s
01 — lists = [[2, 6, 8], [1, 3, 5], [4, 9]] copy 02 — lists = [] copy 03 — lists = [[], []] copy 04 — lists = [[-3, -3], [], [-3, 0]] copy 05 — lists = [[1, 5, 9]] copy 06 — lists = [[-44, -20, 13], [-29, 1, 33, 46], [-42, -29, 21]] copy 07 — lists = [[], [], [-29, -27, 35, 49], [29], [-45, -5, -3, 24, 34]] copy 08 — lists = [] copy 09 — lists = [[-29, -19, 15, 21, 27, 31, 40, 42]] copy 10 — lists = [[], [], []] copy 11 — lists = [[-1, -1, 1, 2, 2, 2, 2], [-2, -2, 1, 1, 2, 2], [-2, -2, -1, -1, -1, 0, 1], [-2, -2, -2, 0, 1, 1, 2]] copy 12 — lists = [[-9768, -9649, -9545, -9535, -8756, -8738, -8607, -8359, -8222, -7771, -7729, -7711, … 100 numbers], [-9859, -9811, -9781, -9670, -9617, -9598, -9495, -8994, -8854, -8582, -8009, -7991, … 100 numbers], [-9291, -9168, -9085, -8866, -8671, -8550, -8096, -8050, -7899, -7643, -7324, -7322, … 100 numbers], [-9968, -9658, -9633, -9490, -9261, -8972, -8880, -8672, -8654, -8381, -7726, -7676, … 100 numbers], [-9857, -9841, -9813, -9786, -9706, -9579, -9559, -9261, -9116, -9068, -8765, -8257, … 100 numbers], [-9958, -9411, -9318, -9311, -9225, -8885, -8842, -8739, -8727, -8066, -7929, -7672, … 100 numbers], [-9788, -9526, -9326, -9128, -9103, -9023, -8980, -8963, -8793, -8702, -8616, -8570, … 100 numbers], [-9944, -9329, -8952, -8614, -8505, -8069, -7974, -7785, -7755, -7698, -7232, -6695, … 100 numbers], [-9981, -9737, -9716, -9499, -9420, -9301, -9179, -8872, -8815, -8713, -8672, -8659, … 100 numbers], [-9881, -9778, -9766, -9561, -9460, -9012, -8742, -8655, -8485, -7740, -7418, -6798, … 100 numbers]] copy 13 — lists = [[-8708, -7219, -4852, -3287], [-9161, -7902, -7455, -6367, -5016, -2437, 1173, 2355, 3261, 3278, 3316, 3389, … 18 numbers], [-4231], [-3130, -581], [-9687, -9071, -6928, -6726, -6115, -3484, 306, 1215, 1642, 2968, 2989, 3162, … 17 numbers], [], [-6367, -2721, -2324, 232, 527, 5146, 5891, 7158], [-6588, -5679, -1079, -499, 1111, 3633, 4131, 6552, 6780, 8630, 9006], [], [-8991, -8054, -7842, -7326, -4506, 1427, 2348, 2862, 3641, 5200, 5489, 6338, … 17 numbers], [-5549, -3727, -1721, 5635, 5796], [386, 5731, 7960, 9032], … 50 arrays] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.