解题中注意的问题:
1手动草稿纸上绘图:
传统思维模式:使用pointer1,pointer2,pointer3交换链表节点。
2 交换节点的时候,注意被交换节点->next的继承,防止丢失
3每次step控制步数。
代码是普通思维模式的下交换链表节点。为了遍历的时候方便,加入虚拟头节点hd。
struct ListNode* swapPairs(struct ListNode* head){
struct ListNode * step1;
struct ListNode * step2;
struct ListNode * step3;
struct ListNode * sp;
struct ListNode * hd;
struct ListNode *cur;
int i=0;
hd=malloc(sizeof(*hd));
hd->next=head;
cur=hd;
while(cur!=NULL&&cur->next!=NULL&&cur->next->next!=NULL)
{
step1=cur;
step2=step1->next;
step3=step2->next;
step2->next=step3->next;
step1->next=step3;
step3->next=step2;
cur=cur->next->next;
}
sp=hd->next;
free(hd);
return sp;
}