1.两两交换链表的结点
其实没啥难度 只是在不断的定义前后中间的四个结点而已 一次while循环即可
/**
* 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) {
ListNode* phead=new ListNode(-1);
phead->next=head;
ListNode* cur=phead;
while(cur->next!=NULL && cur->next->next!=NULL){
ListNode* first=cur->next;
ListNode* second=first->next;
ListNode* latter=second->next;
cur->next=second;
second->next=first;
first->next=latter;
cur=first;
}
return phead->next;
}
};