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;
          }


      }
View Code

 

posted @ 2016-05-19 14:48  牧马人夏峥  阅读(115)  评论(0编辑  收藏  举报