var mergeTwoLists = function(list1, list2) {
const isBaseCase1 = list1 === null;
if (isBaseCase1) return list2;
const isBaseCase2 = list2 === null;
if (isBaseCase2) return list1;
const isL2Greater = list1.val <= list2.val;
if (isL2Greater) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
}
const isL2Less = list2.val <= list1.val;
if (isL2Less) {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
var mergeTwoLists = function(list1, list2) {
let sentinel = tail = new ListNode();
while (list1 && list2) {
const isL2Greater = list1.val <= list2.val;
if (isL2Greater) {
tail.next = list1;
list1 = list1.next;
} else {
tail.next = list2;
list2 = list2.next;
}
tail = tail.next;
}
tail.next = list1 || list2;
return sentinel.next;
};