Skip to content

Commit 1061ab6

Browse files
authored
Merge pull request #838 from nakjun12/main
[nakjun12] Week 4
2 parents 3b55fd8 + ebcfa7c commit 1061ab6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

merge-two-sorted-lists/nakjun12.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function mergeTwoLists(
2+
list1: ListNode | null,
3+
list2: ListNode | null
4+
): ListNode | null {
5+
let head = new ListNode();
6+
let current = head;
7+
8+
while (list1 && list2) {
9+
if (list1.val <= list2.val) {
10+
current.next = list1;
11+
list1 = list1.next;
12+
} else {
13+
current.next = list2;
14+
list2 = list2.next;
15+
}
16+
current = current.next;
17+
}
18+
current.next = list1 || list2;
19+
20+
return head.next;
21+
}
22+
23+
// TC: O(m+n)
24+
// SC: O(1)

0 commit comments

Comments
 (0)