将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
/*** 递归* 时间复杂度:O(n+n)* 空间复杂度:O(n+m)*/var mergeTwoLists1 = function (l1, l2) {if (!l1) return l2if (!l2) return l1if (l1.val < l2.val) {l1.next = mergeTwoLists(l1.next, l2)return l1} else {l2.next = mergeTwoLists(l1, l2.next)return l2}}
/*** 迭代* 时间复杂度:O(n+n)* 空间复杂度:O(1)*/var mergeTwoLists = function (l1, l2) {const pre = new ListNode(null)let curr = prewhile (l1 && l2) {if (l1.val <= l2.val) {curr.next = l1l1 = l1.next} else {curr.next = l2l2 = l2.next}curr = curr.next}curr.next = l1 || l2return pre.next}
