lurenaa的博客

🚌

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head)
return nullptr;
return helper(head, head->next);
}
ListNode* helper(ListNode* head, ListNode* head2) {
if(!head || !head2)
return head;
head->next = head2->next;
head2->next = head;
head->next = helper(head->next, head->next ? head->next->next : nullptr);
return head2;
}
};

Accepted

55/55 cases passed (4 ms)

Your runtime beats 83.69 % of cpp submissions

Your memory usage beats 19.24 % of cpp submissions (8.7 MB)