We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cef4da1 commit 923919bCopy full SHA for 923919b
merge-k-sorted-lists/hoyeongkwak.ts
@@ -0,0 +1,27 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
11
+ */
12
+
13
+function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
14
+ const tempArray: Array<ListNode> = []
15
+ lists.forEach((node) => {
16
+ while (node) {
17
+ tempArray.push(node)
18
+ node = node.next
19
+ }
20
+ })
21
+ tempArray.sort((node1, node2) => node1.val - node2.val)
22
+ let result = tempArray[0] ?? null
23
+ tempArray.forEach((node, index, arr) => {
24
+ node.next = arr[index + 1] ?? null
25
26
+ return result
27
+};
0 commit comments