24. 两两交换链表中的节点

llllmz / 2024-09-27 / 原文

感觉自己畏难情绪好严重呀,一点都不想学新东西。

 

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(!head) return nullptr;
        if(!head->next) return head;
        ListNode* newHead = head->next;
        head->next = swapPairs(newHead->next);
        newHead->next = head;
        return newHead;
    }
};