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.
ListNode *swapPairs(ListNode *head) { // Start typing your C/C++ solution below // DO NOT write int main() function ListNode *p = new ListNode(0); p->next = head; head = p; while(true){ if (p->next==NULL){break;} if (p->next->next==NULL){break;} ListNode* q1 = p->next; ListNode* q2 = q1->next; q1->next = q2->next; q2->next = q1; p->next = q2; p=q1; } return head->next; }
No comments:
Post a Comment