-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |