就是合并两个有序链表了,递归解妥妥儿的。

ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
    if (l1 == NULL) return l2;
    if (l2 == NULL) return l1;
    
    ListNode *ret = NULL;
    
    if (l1->val < l2->val)
    {
        ret = l1;
        ret->next = mergeTwoLists(l1->next, l2);
    }
    else
    {
        ret = l2;
        ret->next = mergeTwoLists(l1, l2->next);
    }
    
    return ret;
}

相关文章:

  • 2021-07-29
  • 2021-06-03
  • 2022-12-23
  • 2021-10-03
  • 2021-10-06
猜你喜欢
  • 2021-10-24
相关资源
相似解决方案