leetcode链接:24. 两两交换链表中的节点 - 力扣(LeetCode)
cpp
struct ListNode * swapPairs(struct ListNode * head)
{
typedef struct ListNode ListNode;
ListNode *fakehead = (ListNode *)malloc(sizeof(ListNode));
fakehead->next = head;
ListNode* right = fakehead->next;
ListNode *left = fakehead;
while(left && right && right->next)
{
left->next = right->next;
right->next = left->next->next;
left->next->next = right;
left = right;
right = left->next;
}
return fakehead->next;
}