标签:AC ack return hang pairs pad pac space ==
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3
.
Note:
struct ListNode* swapPairs(struct ListNode* head) { struct ListNode * p = head; if(head == NULL || head->next==NULL) { return head; } struct ListNode * q = NULL; struct ListNode * prev = NULL; head=head->next; while(p != NULL && p->next != NULL) { q = p->next; p->next = p->next->next; q->next = p; if(prev != NULL) { prev->next = q; } prev = p; p=p->next; } return head; }
标签:AC ack return hang pairs pad pac space ==
原文地址:https://www.cnblogs.com/tuhooo/p/9053887.html