Skip to content

Commit

Permalink
merge-k-sorted-lists solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jdy8739 committed Feb 12, 2025
1 parent d5e98d4 commit c5544a8
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions merge-k-sorted-lists/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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}
*/
var mergeKLists = function (lists) {
const arr = [];

for (let i = 0; i < lists.length; i++) {
let list = lists[i];

while (list) {
arr.push(list.val);
list = list.next;
}
}

if (!arr.length) {
return null;
}

arr.sort((a, b) => a - b);

const first = new ListNode(arr[0]);

let node = first;

for (let j = 1; j < arr.length; j++) {
const next = new ListNode(arr[j]);
node.next = next;
node = next;
}

return first;
};

// 시간복잡도 -> for문 이후 sort 이후 for문을 수행하며 이는 O(n) + O(nlogn) + O(n)이므로 O(nlogn)가 시간복잡도가 된다
// 공간복잡도 -> lists의 노드 수 만큼 길이가 결정되므로 0(n)

0 comments on commit c5544a8

Please sign in to comment.