Skip to content

Commit db34023

Browse files
committed
merge-two-sorted-lists
1 parent 5f6997b commit db34023

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

merge-two-sorted-lists/sooooo-an.ts

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

0 commit comments

Comments
 (0)