struct ListNode* swapPairs(struct ListNode* head){
if(head==NULL) return NULL;
struct ListNode* Head=(struct ListNode*)malloc(sizeof(struct ListNode));
Head->next=head;
struct ListNode* pre;
pre=Head;
while(head&&head->next)
{
struct ListNode* temp=head->next;
head->next=temp->next;
temp->next=head;
pre->next=temp;
pre=head;
head=head->next;
}
return Head->next;
}
思路:首先创建一个空的表头,后面再依次进行交换,最后返回空表头指向的下一个节点。