题目:https://leetcode-cn.com/explore/featured/card/recursion-i/256/principle-of-recursion/1201/
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例:
/**
* 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|| head->next==NULL) return head;
ListNode* p = head->next;
ListNode* temp = p->next;
p->next = head;
head->next = swapPairs(temp);
return p;
}
};