21. 合并两个有序链表 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==nullptr) return l2;
if(l2==nullptr) return l1;
ListNode*head;
if (l1->val>l2->val)
{
head=l2;
head->next=mergeTwoLists(l1, l2->next);
}
else
{
head=l1;
head->next=mergeTwoLists(l1->next,l2);
}
return head;
}
};
206. 反转链表 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur==nullptr) return pre;
ListNode* next = cur->next;
cur->next = pre;
return reverse(cur,next);
}
ListNode* reverseList(ListNode* head) {
return reverse(nullptr,head);
}
};