算法通关村第二关–两两交换链表结点问题解析
思路如图
代码实现
struct ListNode * swapPairs(struct ListNode * head)
{
struct ListNode *dummyHead = (struct ListNode*)malloc(sizeof(struct ListNode));
dummyHead->next=head;
struct ListNode *temp = dummyHead;
while(temp->next !=NULL &&temp->next->next !=NULL)
{
struct ListNode *node1 = temp->next;
struct ListNode *node2 = temp->next->next;
temp->next=node2;
node1->next=node2->next;
node2->next=node1;
temp=node1;
}
return dummyHead->next;
};