Skip to content

Commit 1cba1bd

Browse files
committed
refactor: 💡 import helper function from leetcode 21
1 parent 59bd860 commit 1cba1bd

File tree

1 file changed

+1
-26
lines changed

1 file changed

+1
-26
lines changed

src/0001-1000/23/mergeKLists.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,10 @@
11
import { ListNode } from 'classes/SinglyLinkedListNode'
2+
import { mergeTwoLists } from '0001-1000/21/mergeTwoLists2'
23

34
// <Recursion, DivideAndConquer>
45
// Time: O(knlogk), k is the number of lists
56
// Space: O(logk)
67

7-
function mergeTwoLists(l1: ListNode | null, l2: ListNode | null): ListNode | null {
8-
if (!l1 || !l2) {
9-
return l1 ?? l2
10-
}
11-
12-
const dummyHead = new ListNode(-1)
13-
let tail = dummyHead
14-
let [ptr1, ptr2]: Array<ListNode | null> = [l1, l2]
15-
16-
while (ptr1 && ptr2) {
17-
if (ptr1.val < ptr2.val) {
18-
tail.next = ptr1
19-
ptr1 = ptr1.next
20-
} else {
21-
tail.next = ptr2
22-
ptr2 = ptr2.next
23-
}
24-
25-
tail = tail.next
26-
}
27-
28-
tail.next = ptr1 ?? ptr2
29-
30-
return dummyHead.next
31-
}
32-
338
// 1. divide and conquer
349
function merge(lists: Array<ListNode | null>, left: number, right: number): ListNode | null {
3510
if (left > right) {

0 commit comments

Comments
 (0)