将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
https://leetcode.cn/problems/merge-two-sorted-lists/
var mergeTwoLists = function(list1, list2) {
if (!list1) {
return list2
}
if (!list2) {
return list1
}
var l1 = list1;
var l2 = list2;
var vir = new ListNode(undefined, undefined)
var cursor = vir
while (l1 || l2) {
if (l1 && l2 ) {
if (l1.val < l2.val) {
cursor.next = l1
cursor = l1
l1 = l1.next
} else {
cursor.next = l2
cursor = l2
l2 = l2.next
}
} else if (!l1) {
cursor.next = l2
break
} else {
cursor.next = l1
break
}
}
return vir.next
};