swap nodes in pairs

来源:互联网 发布:如何把矩阵正交化 编辑:程序博客网 时间:2024/06/07 22:01
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *    int val;
 *    ListNode* next;
 *    ListNode(int x): val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == NULL) return NULL;
        if(head->next == NULL) return head;


        ListNode* temp = head->next;
        head->next = swapPairs(temp->next);
        temp->next = head;


        return temp;
    }
};
原创粉丝点击