From 73fb3a11eebbd5ab8e4699e1bc2df2acc41fa4b5 Mon Sep 17 00:00:00 2001 From: nakjun <111031253+nakjun12@users.noreply.github.com> Date: Fri, 3 Jan 2025 22:59:32 +0900 Subject: [PATCH] add mergeTwo solution --- merge-two-sorted-lists/nakjun12.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 merge-two-sorted-lists/nakjun12.ts 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)