Skip to content

Commit

Permalink
Merge pull request #838 from nakjun12/main
Browse files Browse the repository at this point in the history
[nakjun12] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 3b55fd8 + ebcfa7c commit 1061ab6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions merge-two-sorted-lists/nakjun12.ts
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1061ab6

Please sign in to comment.