21. Merge Two Sorted Lists
202407231445
tags: #linked-list
var mergeTwoLists = function(list1, list2) {
let cur1 = list1;
let cur2 = list2;
const dummy = new ListNode(0);
let res = dummy;
while (cur1 !== null && cur2 !== null) {
if (cur1.val < cur2.val) {
res.next = cur1;
cur1 = cur1.next;
} else {
res.next = cur2;
cur2 = cur2.next;
}
res = res.next;
res.next = null;
}
if (cur1 !== null) {
res.next = cur1;
}
if (cur2 !== null) {
res.next = cur2;
}
return dummy.next;
};