Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
思路:将链表拆分,然后再合并
List *swapPairs(List *head) {
if(head == NULL || head->next == NULL)
return head;
List* ji=head;
List* ou = head->next;
List* temp1,*temp2;
List* l1= head;
List* l2 = head->next;
temp1 = l2->next;
while(temp1 != NULL)
{
temp2 = temp1->next;
l1->next = temp1;
l2->next = temp2;
l1 = temp1;
l2 = temp2;
if(l2 !=NULL)
temp1 = l2->next;
else
temp1 = NULL;
}
if(l1 !&#