leetcode 之Swap Nodes in Pairs(21)
不允许通过值来交换,在更新指针时需要小心。
ListNode *swapNodes(ListNode* head) { ListNode dummy(-1); dummy.next = head; for (ListNode *prev = &dummy, *cur = prev->next, *pnext = cur->next; pnext; prev = cur, cur = cur->next, pnext = cur->next) { prev->next = pnext; cur->next = pnext->next; pnext->next = cur; } }