diff --git a/merge-two-sorted-lists/nakjun12.ts b/merge-two-sorted-lists/nakjun12.ts new file mode 100644 index 000000000..03fce8d74 --- /dev/null +++ b/merge-two-sorted-lists/nakjun12.ts @@ -0,0 +1,24 @@ +function mergeTwoLists( + list1: ListNode | null, + list2: ListNode | null +): ListNode | null { + let head = new ListNode(); + let current = head; + + while (list1 && list2) { + if (list1.val <= list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + current.next = list1 || list2; + + return head.next; +} + +// TC: O(m+n) +// SC: O(1)