Skip to content

Commit

Permalink
Merge pull request #510 from gitsunmin/main
Browse files Browse the repository at this point in the history
[gitsunmin] Week 8 Solutions
  • Loading branch information
SamTheKorean authored Oct 6, 2024
2 parents 99a1eb9 + 3de32b3 commit bfe247c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions merge-two-sorted-lists/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* https://leetcode.com/problems/merge-two-sorted-lists
* time complexity : O(n)
* space complexity : O(1)
*/

class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1 && !list2) return null;
if (!list1) return list2;
if (!list2) return list1;

const dummy = new ListNode();
let current = dummy;

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 dummy.next;
};

const input1 = new ListNode(1, new ListNode(2, new ListNode(4)));
const input2 = new ListNode(1, new ListNode(3, new ListNode(4)));

console.log('output1:', mergeTwoLists(input1, input2));
console.log('output2:', mergeTwoLists(input2, input1));

0 comments on commit bfe247c

Please sign in to comment.