Skip to content

Commit 96c1ecb

Browse files
authored
Update solution.ts
1 parent 13b19ba commit 96c1ecb

File tree

1 file changed

+38
-41
lines changed
  • src/main/ts/g0001_0100/s0023_merge_k_sorted_lists

1 file changed

+38
-41
lines changed
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List
22
// #Divide_and_Conquer #Merge_Sort #Big_O_Time_O(k*n*log(k))_Space_O(log(k))
3-
// #2023_08_29_Time_75_ms_(97.06%)_Space_47.3_MB_(98.42%)
3+
// #2023_10_09_Time_76_ms_(94.52%)_Space_47.9_MB_(84.35%)
44

5-
/**
5+
import { ListNode } from '../../com_github_leetcode/listnode'
6+
7+
/*
68
* Definition for singly-linked list.
79
* class ListNode {
810
* val: number
@@ -13,51 +15,46 @@
1315
* }
1416
* }
1517
*/
16-
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
17-
if (lists.length === 0) {
18-
return null
18+
const merge2Lists = (list1: ListNode | null, list2: ListNode | null): ListNode | null => {
19+
if (!list1 || !list2) {
20+
return list1 || list2
1921
}
20-
return mergeKListsRecursive(lists, 0, lists.length)
21-
}
22-
23-
function mergeKListsRecursive(lists: Array<ListNode | null>, leftIndex: number, rightIndex: number): ListNode | null {
24-
if (rightIndex > leftIndex + 1) {
25-
const mid = Math.floor((leftIndex + rightIndex) / 2)
26-
const left = mergeKListsRecursive(lists, leftIndex, mid)
27-
const right = mergeKListsRecursive(lists, mid, rightIndex)
28-
return mergeTwoLists(left, right)
29-
} else {
30-
return lists[leftIndex]
22+
const tempHead = new ListNode()
23+
let current = tempHead
24+
let l1 = list1
25+
let l2 = list2
26+
while (l1 || l2) {
27+
if (!l1) {
28+
current.next = l2
29+
break
30+
}
31+
if (!l2) {
32+
current.next = l1
33+
break
34+
}
35+
if (l1.val < l2.val) {
36+
current.next = l1
37+
l1 = l1.next
38+
} else {
39+
current.next = l2
40+
l2 = l2.next
41+
}
42+
current = current.next
3143
}
44+
return tempHead.next
3245
}
3346

34-
function mergeTwoLists(left: ListNode | null, right: ListNode | null): ListNode | null {
35-
if (left === null) {
36-
return right
37-
}
38-
if (right === null) {
39-
return left
40-
}
41-
let res: ListNode | null
42-
if (left.val <= right.val) {
43-
res = left
44-
left = left.next
45-
} else {
46-
res = right
47-
right = right.next
48-
}
49-
let node = res
50-
while (left !== null || right !== null) {
51-
if (right === null || left.val <= right.val) {
52-
node.next = left
53-
left = left.next
54-
} else {
55-
node.next = right
56-
right = right.next
47+
const mergeKLists = (lists: Array<ListNode | null>): ListNode | null => {
48+
while (lists.length > 1) {
49+
const mergedLists = []
50+
for (let i = 0; i < lists.length; i += 2) {
51+
const list1 = lists[i]
52+
const list2 = lists[i + 1] ?? null
53+
mergedLists.push(merge2Lists(list1, list2))
5754
}
58-
node = node.next
55+
lists = mergedLists
5956
}
60-
return res
57+
return lists[0] ?? null
6158
}
6259

6360
export { mergeKLists }

0 commit comments

Comments
 (0)