Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 24. Swap Nodes in Pairs
參考資訊:
https://www.cnblogs.com/grandyang/p/4441680.html
題目:

解答:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) {
return head;
}
ListNode* t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
};