Original link: leetcode-cn.com/problems/me…

Answer:

Please refer to the official solution.

/ * * *@param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}* /
var mergeTwoLists = function (l1, l2) {
  // If L1 is null, no need to continue traversal.
  // just return the remaining nodes of L2
  if(! l1) {return l2;
  }
  if(! l2) {return l1;
  }
  if (l1.val <= l2.val) {
    // At this point l1 can be merged to the new node.
    // l1.next points to l1.next, which is smaller than l2
    l1.next = mergeTwoLists(l1.next, l2);
    return l1; // Return the node currently used for sorting
  } else {
    L2. val>l1.val
    // l2.next points to a smaller l1 than l2.next
    l2.next = mergeTwoLists(l1, l2.next);
    return l2; // Return the node currently used for sorting}};Copy the code